oop - When do we return values in PHP functions? -
sorry question don't understand how works:
class person { public static $age = 1; public function havebirthday() { static::$age +=1; } } $joe = new person; $joe->havebirthday(); echo person::$age; what i'm not understanding this:
public function havebirthday() { static::$age +=1; } isn't supposed return $age otherwise value lost? why still working?
thanks!
you've defined static, means class level variables instead of instance level.
so when call $joe->havebirthday(); updates class level variable of person class can accessed using person::$age;.
static variables not need returned, can access directly class.
Comments
Post a Comment