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???

Link to comment
Share on other sites

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
}

Link to comment
Share on other sites

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

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.