JustinK101 Posted May 15, 2011 Share Posted May 15, 2011 If I have a base class MyBase: class MyBase { public $user_id = NULL; public function __construct() { $this->user_id = "justin"; } } Then I have a class which inherits the base class MyClass: class Test extends MyBase { public static function get_user_id() { echo parent::$user_id; } } Finally calling it: echo Test::get_user_id(); First question, do I have to create an instance of MyBase, or when Test extends MyBase will it instanciate it automatically and call the MyBase constructor? Second, I am getting the error: PHP Fatal error: Access to undeclared static property: MyBase::$user_id Quote Link to comment https://forums.phpfreaks.com/topic/236450-oo-extends-with-a-constructor/ Share on other sites More sharing options...
gizmola Posted May 15, 2011 Share Posted May 15, 2011 As long as the child class does not have a constructor the parent constructor will be called. If you're going to try and access a variable statically, it has to be declared to be static. Quote Link to comment https://forums.phpfreaks.com/topic/236450-oo-extends-with-a-constructor/#findComment-1215617 Share on other sites More sharing options...
JustinK101 Posted May 15, 2011 Author Share Posted May 15, 2011 Gizmola, First, who is calling the base constructor? I actually don't want to access user_id statically, but I think to use the keyword `parent` you have to use it like: parent::$user_id. Is there another way to access the property of the MyBase class $user_id from inside the Test class? Quote Link to comment https://forums.phpfreaks.com/topic/236450-oo-extends-with-a-constructor/#findComment-1215618 Share on other sites More sharing options...
gizmola Posted May 15, 2011 Share Posted May 15, 2011 class MyBase { public $user_id = NULL; public function __construct() { $this->user_id = "justin"; } } class Test extends MyBase { public function get_user_id() { echo $this->user_id; } } $t = new Test(); $t->get_user_id() Quote Link to comment https://forums.phpfreaks.com/topic/236450-oo-extends-with-a-constructor/#findComment-1215621 Share on other sites More sharing options...
JustinK101 Posted May 15, 2011 Author Share Posted May 15, 2011 The only thing, is that I don't want Test to be an instance. It is static. So: class Test extends MyBase { public static function get_user_id() { echo $this->user_id; } } Obviously, if its static, can't use $this. Is there a way around this? Quote Link to comment https://forums.phpfreaks.com/topic/236450-oo-extends-with-a-constructor/#findComment-1215622 Share on other sites More sharing options...
gizmola Posted May 15, 2011 Share Posted May 15, 2011 You can use "self". However, php does not have static constructors, so code you put in the parent class constructor will never execute, if that's what you're after. Quote Link to comment https://forums.phpfreaks.com/topic/236450-oo-extends-with-a-constructor/#findComment-1215625 Share on other sites More sharing options...
ignace Posted May 15, 2011 Share Posted May 15, 2011 Why do you want to call it statically? To access $user_id you need to declare it statically. class MyBase { protected static $user_id = "justin"; } class Test extends MyBase { public static function get_user_id() { return self::$user_id; // or static::$user_id (5.3.x+) or parent::$user_id; } } print Test::get_user_id(); Quote Link to comment https://forums.phpfreaks.com/topic/236450-oo-extends-with-a-constructor/#findComment-1215626 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.