I am trying to learn oop in php, but the below code is not working could someone provide an a开发者_StackOverflowlternate answer?
<?php
class abc {
public $a = 1;
$b = $a;
function foo(){
//some function..
}
}
?>
I want to assign value of variable "a" to variable "b".
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.
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)
}
精彩评论