Jump to content

[SOLVED] PHP4 OOP Newbie question: Pointer to parent object


Raj Singh

Recommended Posts

Maybe some example code of what you mean might help define this problem better.

 

Here's my attempt at examplifying what you described

// Class A
class A {
private $internalVar = "string1";

// Get internal Var.
public function getInternalVar(){
   return $this->internalVar;
}
}

// Class B
class B {
// Private variable of type A
private $aObject;

// Set the private internal variable to an instanceof A.
public function setAObject(A $a){
   $this->aObject = $a;
}

// Method call to A::getInternalVar()
public function getInternalVar(){
   return $this->aObject->getInternalVar();
}
}

$classA = new A();
$classB = new B();
$classB->setAObject($a);
echo $classB->getInternalVar();

 

Is that what you meant? Or you really talking about inheritance???

Thank you for helping me.

First, I'm using PHP 4 but it might not be an issue.

Here's what I had in mind

 

$Foo = new Foo();

class Foo
{
var $MyObject;
function Foo
{
  $this->MyObject = new Bar();
}
}

class Bar
{
var $PointerToObjectFoo;//Here I want this variable to be a pointer to the $Foo Object of the Main
}

Yes it's do-able.

I recommend changing to PHP5 because otherwise you're going to have issues with objects/classes NOT being references like they are in 5.

$Foo = new Foo();

class Foo
{
var $MyObject;
function Foo
{
  $this->MyObject =& new Bar($this);
}
}

class Bar
{
var $PointerToObjectFoo;//Here I want this variable to be a pointer to the $Foo Object of the Main

function Bar($foo){
   // Create reference to Object of type Foo
   $this->PointerToObjectFoo =& $foo;
}
}

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.