Millar Posted January 24, 2008 Share Posted January 24, 2008 Hello, Objects which are static in classes can be accessed with parent::$obj and thus $this cannot be used within the object. So what is the non-static equivilent I can use to access the classes parent in a non-static way? When I try to use the static method (i.e. parent::$obj) it errors out: "Fatal error: Access to undeclared static property: class::$obj in [...]". Thanks. Link to comment https://forums.phpfreaks.com/topic/87573-solved-non-static-parent-equivilent/ Share on other sites More sharing options...
rhodesa Posted January 24, 2008 Share Posted January 24, 2008 Your wording is a little unclear, can you provide some example code of what you are doing. Link to comment https://forums.phpfreaks.com/topic/87573-solved-non-static-parent-equivilent/#findComment-447899 Share on other sites More sharing options...
Millar Posted January 24, 2008 Author Share Posted January 24, 2008 Okay, here is a code example: class base { var $var; function __construct ( $var ) { $this->var = $var; } function func ( ) { print $this->var; } } class stend extends base { var $var; //Different var specific to the stend class. function need_var ( ) { return --THIS IS THE VALE-- } } $class = new base ( "toenail" ); $stended = new stend; print $stended->need_var(); I know that for that example you could change it and remove the need for the other classes, but in my case I need the classes, so I have the problem presented there. So where it says "--THIS IS THE VALE--" how can I reference the $var var from the base class? Link to comment https://forums.phpfreaks.com/topic/87573-solved-non-static-parent-equivilent/#findComment-447908 Share on other sites More sharing options...
rhodesa Posted January 24, 2008 Share Posted January 24, 2008 You can't...$class and $stend are completely independent objects. I think you are looking for something more like this: <?php class base { var $var; function __construct ( $var ) { $this->var = $var; } function func ( ) { print $this->var; } } class stend { var $child; function __construct ( $var_obj ) { $this->child = $var_obj; } function need_var ( ) { return $this->child->var; } } $base= new base ( "toenail" ); $stended = new stend($base); print $stended->need_var(); ?> Link to comment https://forums.phpfreaks.com/topic/87573-solved-non-static-parent-equivilent/#findComment-447919 Share on other sites More sharing options...
Millar Posted January 24, 2008 Author Share Posted January 24, 2008 Thanks, does just what I want. Link to comment https://forums.phpfreaks.com/topic/87573-solved-non-static-parent-equivilent/#findComment-447921 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.