php - Increase static class variable in a constructor -
hi i've got following simple problem:
i'd increase static class variable each time instance of object created. i've tried this:
class task { static $tid = 0; function __construct() { $this->tid++; } }
this returns following error message:
notice: undefined property: task::$tid in ... on line ...
how correctly? thx in advance :)
edit:
sorry, clear approach cant work, cause static keyword indicates property not bound instance, -> doesnt work. how correctly?
$tid++
doesnt work. error: undefined variable.
as per php manual:
static properties cannot accessed through object using arrow operator ->. other php static variable, static properties may initialized using literal or constant before php 5.6; expressions not allowed.
there way. must use self
keyword:
class task { static $tid = 0; function __construct() { self::$tid++; } } new task(); new task(); echo task::$tid;
will output 2
Comments
Post a Comment