Not defined Substr in PHP - php

I tried substr() method in PHP 5.6 to get some part of String,
first,
<?php
echo substr("Avicienna", 0,3);
and save it to a file.
second one,
<?php
class Test{
public function index(){
$name = "Hasan";
var_dump(subtr($name,0,3));
}
}
$test = new Test();
$test->index();
and save it to another file.
the first one without class, return correct string parts, while, the second one return PHP 500 error :
Call to undefined method Coba::subtr() in /var/www/html/koper/coba.php on line 5
is there any limitation to call substr() or others php function inside a class ?

Typo error subtr() . Its substr().

Related

I get this error "Function name must be a string"

I have a PHP class where one of the private member is a callback to my log function (i.e. in PHP land, a function pointer is simply a string containing the name of the function to call).
self::$logferr = "msgfunc";
self::$logferr($res);
I get this error:
Fatal error: Function name must be a string
self::$logferr is equal to "msgfunc" which is my log function.
If I rewrite the code like this (on the same very class method):
$tmp = "msgfunc";
$tmp($res);
It works, the log function get called
Just wrap your variable in parenthesis, let PHP resolve the value first:
(self::$logferr)($res);
Proof of concept
You can use call_user_func. ref: this
call_user_func(self::$logferr, $res);
You should call it by using
self::{self::$logferr}($req)
Working example : https://3v4l.org/CYURS
Let's build a reproducible example:
class Foo {
private static $_loggerCallback;
static function setLogCallback(callable $loggerCallback) {
self::$_loggerCallback = $loggerCallback;
}
static function log(...$arguments) {
if (NULL !== self::$_loggerCallback) {
return self::$_loggerCallback(...$arguments);
}
return NULL;
}
}
Foo::setLogCallback(function() { echo 'success'; } );
Foo::log();
Output:
Notice: Undefined variable: _loggerCallback in /in/f3stL on line 13
Fatal error: Uncaught Error: Function name must be a string in /in/f3stL:13
The notice reports the actual mistake in this case. If you do not get something like it, you should check your error reporting configuration.
The notice shows that PHP looks for a local variable $_loggerCallback. It tries to execute $_loggerCallback(...$arguments). Here are different possibilities to make the call explicit.
Use parenthesis (PHP >= 7.0):
return (self::$_loggerCallback)(...$arguments);
Use a local variable (as you did):
$callback = self::$_loggerCallback;
return $callback(...$arguments);
A small advise. PHP support anonymous functions. You do not need a (global) function for a callback. This avoids calling to the function by name as well.

Php object return null value while echo is working

i took a class and function and determine size of file.but when i echo the size and call the function it works but when i store it in variable and call via object it shows null value,whats wrong can be there?
when i use
echo $currentsize;
it gives me value but object does not store any thing.
<?php
class Mainc {
public $sizenow;
function myprocess(){
$currentsize= 1.20;
echo $currentsize;
$this->size = $currentsize;
}
}
$objsize=new Mainc();
var_dump($objsize);
Your $sizenow is not initialized unless you call smprocess() which your example code does not do. Alter it to:
$objsize=new Mainc();
$objsize->smprocess();
var_dump($objsize);
There are two problems I see in your code. First is that you are not called smprocess() method. Also you are using $this->size_now and the property is defined public $sizenow;
I believe this will fix your code

PHP Passing function as a parameter syntax

can someone pls fill me in on the correct syntax for this in PHP? I have:
function mainFunction($str,$thingToDoInMain){
$thingToDoInMain($str);
}
function printStr($str){
echo $str;
}
mainFunction("Hello",printStr);
I am running this on WAMP and I get an error/warning saying
use of undefined constant printStr - assumed 'printStr' on line 5
...and then I get "Hello" printed out as desired further down the page.
So, how do a refer to the function printStr in the last line to get rid of warning? I have tried $printStr, printStr() and $printStr. Thanks in advance.
Simply add a $ sign before your thingToDoInMain function call making it a proper function name, because thingToDoInMain itself is not a function name. Whereas the value contained in $thingToDoInMain is a function name.
<?php
function mainFunction($str,$thingToDoInMain){
$thingToDoInMain($str); //Here $ is missing
}
function printStr($str){
echo $str;
}
mainFunction("Hello","printStr"); // And these quotes
?>
Demo
In PHP, function names are passed as strings.
mainFunction("Hello", "printStr");

PHP Basics: Can't deal with scope within classes

i got some trouble to understand scope in OOP. What i want is that $foo->test_item() prints "teststring"...Now it just fails with:
Warning: Missing argument 1 for testing::test_item()
Thanks a lot!
<?php
class testing {
public $vari = "teststring";
function test_item($vari){ //$this->vari doesn't work either
print $vari;
}
}
$foo = new testing();
$foo->test_item();
?>
test_item() should be:
function test_item() {
print $this->vari;
}
There is no need to pass $vari as a parameter.
Well, you've declared a method which expects an argument, which is missing. You should do:
$foo->test_item("Something");
As for the $this->, that goes inside of the class methods.
function test_item(){
print $this->vari;
}
function parameters can not be as "$this->var",
change your class like
class testing {
public $vari = "teststring";
function test_item(){ //$this->vari doesn't work either
print $this->vari;
}
}
$foo = new testing();
$foo->test_item();
And read this Object-Oriented PHP for Beginners
What's happening there is that $foo->test_item() is expecting something passed as an argument, so for example
$foo->test_item("Hello");
Would be correct in this case. This would print Hello
But, you may be wondering why it doesn't print teststring. This is because by calling
print $vari;
you are only printing the variable that has been passed to $foo->test_item()
However, if instead you do
function test_item(){ //notice I've removed the argument passed to test_item here...
print $this->vari;
}
You will instead be printing the value of the class property $vari. Use $this->... to call functions or variables within the scope of the class. If you try it without $this-> then PHP will look for that variable within the function's local scope

PHP: Use function outside current function

can I use a function that is outside the current function?
eg.
function one($test){
return 1;
}
function two($id){
one($id);
}
Seems like i cant, how should I do it then to use the function that are outside? Thanks
The function is in the same file.. /
Is your function inside of a class? In that case you have to use $this->function() instead of function().
That's perfectly valid. Check out the running code here.
Your code looks valid to me : you are declaring two functions, called one and two ; and two is calling one.
Then, you can call any of those functions, to execute it.
For example, if you execute the following portion of code :
function one($test){
var_dump(__FUNCTION__);
return 1;
}
function two($id){
var_dump(__FUNCTION__);
one($id);
}
two('plop');
Note that I called two, in the last line of this example.
You'll get this kind of output :
string 'two' (length=3)
string 'one' (length=3)
Which shows that both functions were executed.
That works fine. However, one ignores its parameter. Then, two ignores the return value from one.
This should work fine
Example:
<?php
function test ($asd)
{
return $asd;
}
function run ()
{
return test('dd');
}
echo run();
?>
Maybe you have an issue elsewhere?

Categories