Using class object and method in function
Posted: 11 Oct 2017, 14:45
Hello,
I try to create and use a class object instance and its method inside a function.
For example I have the following code, but it not works:
It shows this error:
I try to create and use a class object instance and its method inside a function.
For example I have the following code, but it not works:
Code: Select all
class clsTest {
protected $prop ='';
function __construct($prop){
$this->prop = $prop;var_dump($prop);
}
public function method($str){
return $this->prop .' / '. $str;
}
}
//Receives $prop-argument for object-instance, $str-argument for method()
function fun(){
$str ='Text to method.';
$ob = new clsTest($prop);
$re = $ob->method($str);
return $re .' / Text from function.';
}
$prop ='Prop for class.';
echo fun();
Code: Select all
Notice: Undefined variable: prop