5kyy8lu3 Posted March 16, 2010 Share Posted March 16, 2010 Hi. I'm in the process of learning OOP. I had a question. What's the proper way for a parent class to access properties of a child class? Should I pass the object in as a function argument? Then access it that way? I can't really think of any better way that? This method wouldn't break encapsulation, would it? Example of what I'm asking: <?php class A { function testIt($class) { if ( $class->getTest() == 1 ) { echo 'Yes!'; } else { echo 'No!'; } } } class B extends A { var $Test; function setTest($value) { $this->Test = $value; } function getTest() { return $this->Test; } } $classA = new A; $classB = new B; $classB->setTest(1); $classA->testIt($classB); ?> Quote Link to comment Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 Hopefully this helps a bit... <?php $class = new A(); $class->testIt(); b::testing2(); class A { public function testIt() { if(1 == b::isItTrue) { echo "yes!"; } else { echo "no!"; } } } class b extends A { const isItTrue = 1; static public function testing2() { echo "Envoked a non-instantiated static method!<br />"; } } ?> Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted March 16, 2010 Author Share Posted March 16, 2010 much appreciated! Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted March 16, 2010 Author Share Posted March 16, 2010 Another quick question! Besides using a getter method to pull a value out of one object, and then sending that value into another object with a setter method, is there any other "proper" way of getting the value of a property from one class or object to another? Quote Link to comment Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 Sure there is. You can call properties in the same way you would call an non-instatiated method. The real key to the example I gate was: b::isThisTrue You can call that from outside the class, or from other classes, as long as the property is public. Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted March 16, 2010 Author Share Posted March 16, 2010 Sure there is. You can call properties in the same way you would call an non-instatiated method. The real key to the example I gate was: b::isThisTrue You can call that from outside the class, or from other classes, as long as the property is public. I can't get it to work. I can only get it to work with a constant, but what I need is to pass variables. Here's my test code: <?php class A { var $Test; public function setTest($Inbound) { $this->Test = $Inbound; } public function getTest() { return $this->Test; } } class B { public function attempt1() { echo A::getTest(); } public function attempt2() { echo A::$Test; } } $A = new A; $B = new B; $A->setTest("1234"); $B->attempt1(); //returns nothing when I try to use a method to get the value $B->attempt2(); //gives me error: Undefined class constant 'Test' in /var/www/test.php on line 24 ?> UPDATE: oh, and I even tried making the method static but the problem is that both classes will be instantiated and it seems using static is only for when it's not going to be instantiated Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted March 16, 2010 Author Share Posted March 16, 2010 actually nevermind, I think I'll just manually use a getter to pull the value out and send it in to the other object manually. directly accessing another object's properties just seems like it goes against encapsulation standards. i was just being lazy but i'd prefer to do it the correct way lol, so i guess i'll just pull a value with a getter method and send it into the other object with a method. Quote Link to comment Share on other sites More sharing options...
ksugihara Posted March 16, 2010 Share Posted March 16, 2010 <?php b::showTest(); class A { var $Test = 1; } class b extends A { static public function showTest(){ $class = new A(); echo $class->Test; } } ?> Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted March 16, 2010 Author Share Posted March 16, 2010 problem with that method is that I'm wanting to access a property of a class that's already been instantiated outside of that class so if I create a new instance of the class the properties won't be the same. example: <?php class A { var $Test; public function setTest($Input) { $this->Test = $Input; } } class B { static public function showTest(){ $class = new A(); echo $class->Test; } } $A = new A; $B = new B; $A->setTest("1234"); B::showTest(); ?> Quote Link to comment Share on other sites More sharing options...
trq Posted March 16, 2010 Share Posted March 16, 2010 What exactly are you trying to do? Children inherit from a parent, not the other way around. Quote Link to comment Share on other sites More sharing options...
5kyy8lu3 Posted March 16, 2010 Author Share Posted March 16, 2010 What exactly are you trying to do? Children inherit from a parent, not the other way around. just wanted to access the property of one object from another object. non related objects. (no parent/child) i kinda figured on my own that i should just use a getter/setter since any direct access between the two objects would break encapsulation. Quote Link to comment Share on other sites More sharing options...
trq Posted March 17, 2010 Share Posted March 17, 2010 just wanted to access the property of one object from another object. Unless your using static properties / methods you will need to instantiate the object you want to access within the other object, or pass it into the object somehow. Quote Link to comment 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.