Reputation: 907
I am trying to learn oop in php, but the below code is not working could someone provide an alternate answer?
<?php
class abc {
public $a = 1;
$b = $a;
function foo(){
//some function..
}
}
?>
I want to assign value of variable "a" to variable "b".
Upvotes: 0
Views: 126
Reputation: 96159
Not exactly what you've asked for but it might (or might not) solve your problem:
You can define a constant that is used to initialize both members, $a and $b.
<?php
class abc {
const defaultValue = 1;
public $a = self::defaultValue;
public $b = self::defaultValue;
function foo(){
//some function..
}
}
$abc = new abc;
var_dump($abc);
prints
object(abc)#1 (2) {
["a"]=>
int(1)
["b"]=>
int(1)
}
Upvotes: 1
Reputation: 73282
You can assign the value of $a
to $b
like so: $this->b = $this->a
within the __construct
method which gets called upon object creation, assuming you're running PHP 5.
Upvotes: 3