Jump to content

object in object seting values


ckrudelux

Recommended Posts

class parent {public $child;public $somevalue;function __construct(){$this->child = new child();}private function dosomestuff(){//somestuff}}

class child {function __construct(){//some other stuff..}public function otherfunction(){//set $somestuff from parent}}

Called outside the objects:

$parent->child->otherfunction();

Is it posible to set $somestuff from child class or do I have to make the call different in parent so parent can get the value?all I found searching was get_class which only get's me the name of the class and not the instance :/

Link to comment
Share on other sites

i cant see any $somestuff neither in parent class nor in child class though.. According to your code you cant access the property of parent class from child class. If you want to set a property of parent class in a function of child class you need to initiate a object of parent class in child class if you dont initiate a object how can your child class function know which objects property have to set. I am not sure about your purpose but if you want a non instance property which is not depend on instances you can set the property $somestuff as static public so that you will be able to set that property from child class.

Link to comment
Share on other sites

i cant see any $somestuff neither in parent class nor in child class though.. According to your code you cant access the property of parent class from child class. If you want to set a property of parent class in a function of child class you need to initiate a object of parent class in child class if you dont initiate a object how can your child class function know which objects property have to set. I am not sure about your purpose but if you want a non instance property which is not depend on instances you can set the property $somestuff as static public so that you will be able to set that property from child class.
my bad should be $somevalue..Anyway, why I want to do this is cause I have a class geting page data, and a class geting post for the page but then it's only one post to show the post it can overwrite the template should be used but to change the template I need to overwrite the variable in the page class, both the content class and page class is loaded form a root class so if I can gain access to the root class I can change the value in the page class.As far as I know static won't work cause it wont effect the object and if it did it would be a new instance of object. I want the calling instance.if the class would be global I could get the instance. Calling the global from the child class.Maybe I could send a reference to the child __construct which points to the parent but I don't know how what would look like maybe something like:
$this->child = new child($this);

Link to comment
Share on other sites

Maybe I could send a reference to the child __construct which points to the parent but I don't know how what would look like maybe something like:
$this->child = new child($this);

That's what I was going to recommend. You have the correct syntax. Something like this seems to do what you're asking:
class ParentClass {	public $Child;	public $value;	public function __construct() {		$this->Child = new ChildClass($this);	}	public function setValue($newValue) {		$this->value = $newValue;	}}class ChildClass {	public $Parent;	public function __construct($parent) {		$this->Parent = $parent;	}	public function setParentValue($newValue) {		$this->Parent->setValue($newValue);	}}

Link to comment
Share on other sites

That's what I was going to recommend. You have the correct syntax. Something like this seems to do what you're asking:
class ParentClass {	public $Child;	public $value;	public function __construct() {		$this->Child = new ChildClass($this);	}	public function setValue($newValue) {		$this->value = $newValue;	}}class ChildClass {	public $Parent;	public function __construct($parent) {		$this->Parent = $parent;	}	public function setParentValue($newValue) {		$this->Parent->setValue($newValue);	}}

Busy,busy :) Sorry for no replay, but something else came up and I almost forgot I put this question on the board. The answer is very appreciated, haven't tested your code but looks like it would work. :)
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...