Jump to content

Parent class accessing properties of a child class


5kyy8lu3

Recommended Posts

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);

?>

Link to comment
Share on other sites

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 />";
}
}
?>

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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();

?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.