I want to set my value in object using public method. But I can't find to make it work
<?php
class User{
public $id;
public function ToSynchData(){
$this->$id = "1";
}
}
$new = new User;
$new->ToSynchData();
$new->$id;
?>
public $id;
public function ToSynchData(){
$this->id = 1;
}
$new = new User;
$new->ToSynchData();
echo $new->id; // 1
EDIT: Why static all of a sudden?
class User{
public $id;
public function ToSynchData(){
$this->id = "1";
}
}
$new = new User();
$new->ToSynchData();
print_r($new->id);
you are trying to access static property with ->
whereas it is clearly written here
Static properties cannot be accessed through the object using the arrow operator ->.
therefore to access it you have to change it from static public
to public