Given this class :
class Address {
public $id;
public $id_easypost;
public $street1;
public $street2;
public function __construct($id,$id_easypost,$street1,$street2) {
$this->$id = $id;
$this->$id_easypost = $id_easypost;
$this->$street1 = $street1;
$this->$street2 = $street2;
}
}
I don't get why, when creating an object like that:
$ad = new Address("1", "2", "3", "4");
Values are not "fetched" correctly :
object(Address)[15]
public 'id' => null
public 'id_easypost' => null
public 'street1' => null
public 'street2' => null
public '1' => string '1' (length=1)
public '2' => string '2' (length=1)
public '3' => string '3' (length=1)
public '4' => string '4' (length=1)
However, this class works correctly :
class Rider {
public $id;
public $name;
public $activated;
public $created_at;
public $updated_at;
public function __construct($id, $name, $activated, $created_at, $updated_at) {
$this->id = $id;
$this->name = $name;
$this->activated = $activated;
$this->created_at = $created_at;
$this->updated_at = $updated_at;
}
}
And "fetch" the values correctly.
object(Rider)[16]
public 'id' => string '1' (length=1)
public 'name' => string '2' (length=1)
public 'activated' => string '3' (length=1)
public 'created_at' => string '4' (length=1)
public 'updated_at' => string '5' (length=1)
How is that ?
You shouldn't use $
sign to access object properties. This is correct:
$this->id = $id;