Why I am getting error in this?
I want to save $a (outside the class), to $b (inside the class).
$a= '5';
class abc {
public $b = $a;
echo $b;
}
Error is Parse error: parse error, expecting `T_FUNCTION’
Why I am getting error in this?
I want to save $a (outside the class), to $b (inside the class).
$a= '5';
class abc {
public $b = $a;
echo $b;
}
Error is Parse error: parse error, expecting `T_FUNCTION’
I also tried t his, but didn’t find any solutions yet.
$a= ‘5’;
class abc {
global $a;
public $b = $a;
echo $b;
}
This is the correct way of working in OOP:
define('_A', '5');
class abc {
public $b = _A;
// or
//public $b = '5';
public function __construct(){
// do some default stuffs here
}
public function printVal(){
echo $this->b;
}
}
$obj = new abc;
$obj->printVal();
Thank you sir.
I went upset when it was not working but you gave me the hope to work.
Thanks again sir.
Really Rajug? (:
OP - you shouldn’t be using object approach if you have no clue what it’s about.
And as Anthony insinuated, it’s not the right way to do it when using object oriented approach. In fact, it’s pointless to do it that way.
I am only pointing how to echo the values (by having a separate method) in the class. And I am not saying this is the best way but I said ‘correct way’ because it does not have errors ( ). So this can be done in different way depending upon the requirement.