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

?>

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

 

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?

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.

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

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.

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

?>

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.

Archived

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

×
×
  • 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.