Jump to content

OOP - References and child classes help


linchat

Recommended Posts

Hi all, wandering if someone could give me a hand. I have a a parent and child class which I would like data to be shared. So I am using references (Using PHP 5.0.4). I also want the objects in the parent class protected so that only the constructor can set the data. My problem is, when I reference the objects from the child class I get an error that say Cannot access private property.... I think I designed this right, but who knows. Here is an example.

 

class a

{

    protected $one;

    protected $two;

 

    function a()

    {

          $this->one = 1;

          $this->two = 2;

    }

}

 

class b extends a

{

function b(&$obj)

{

$this->one = &$obj->one;

$this->two = &$obj->two;

}

}

 

$apples = &new a();

$oranges = new b($apples);

 

I would like class $oranges to use the data / reference of $apples. I want the classes themselves (a's constructor) to set the data but allowing b to read / write. Nothing changing data outside the classes. Protected is supposed to do this, unless my codes is screwed. Pretty new to OOP.

 

I am not even sure if I developed Class B correctly. I want to access the reference, the only way I could do it was by creating a constructor for it. I guess that is right.

 

Thanks in advance.

Link to comment
https://forums.phpfreaks.com/topic/49720-oop-references-and-child-classes-help/
Share on other sites

"These are not the $one, $two you are looking for"...

 

When you make a new class A and then pass it to class B they have nothing to do with eachother.. just because B extends A it is a completely different instance of A, therefore it cannot access it's private variables... make them public if you want them to be usable in this way or you could create a getVar() method that is public... eg...

 

<?php
class a
{
    protected $one;
    protected $two;

    public function a()
    {
           $this->one = 1;
           $this->two = 2;
    }

    public function getOne()
    {
        return $this->one;
    }

    public function getTwo()
    {
        return $this->two;
    }
}

class b extends a
{
   function b($obj)
   {
      $this->one = $obj->getOne();
      $this->two = $obj->getTwo();
   }
}
?>

 

PS, you no longer have to pass objects by reference, php5 does this automatically.. I'd also suggest using __construct() instead of the class name for your construct (although both technically work)

Thank you, it makes sense... I have one last question which I am a bit confused on and I do not know how to accomplish, searched everywhere for the answer.

 

How do I use the reference in my child class? Do I have to construct all of the variables again? If you see in my original example, I passed the refence to the child, the constructed the variables, is this correct form?

I think what you mean to do is

 

<?php
class a
{
    protected $one;
    protected $two;

    public function __construct()
    {
           $this->one = 1;
           $this->two = 2;
    }
}

class b extends a
{
    public function echoOne()
    {
        echo $this->one;
    }
}


$b = new b();
$b->echoOne(); // outputs 1, the child inherits it's parent's construct
?>

 

edit: fixed a quick problem with it :P

<?php

class a

{

    public $one;

    public $two;

    public $four;

 

    public function __construct()

    {

          $this->one = 1;

          $this->two = 2;

    }

}

 

class b extends a

{

}

?>

 

$a = new a();

$a->four = 15;

 

$b = new b();

echo $b->four;

 

At this point I would want $b to print 15, based on the reference I pass from $a.

 

How do I reference the value reference of $a->four within class b? Basically, I guess, how do you reference referenced data in a child class that has been passed an object from its parent. What is the proper way to pass a reference or object to "b" from "a" and have access to its data?

 

 

I still don't know what you mean.. heh... something like this?

 

<?php
class a
{
    public $one;
    public $two;
    public $four;

    public function __construct()
    {
           $this->one = 1;
           $this->two = 2;
    }
}

class b extends a
{
}

$b = new b();
$b->four = 15;
echo $b->four;
?>

 

As I said before $a and $b are complete separate.. since they're completely different instances of the objects it's like they aren't even related.

OK... An example, maybe I am doing things wrong and further more I KNOW I am not explaining it correctly... :) Real world example.

 

Lets say I have a "user" class which controls user info and contains the vars/properties...

 

Now I want to create a "records" class which extends users...

 

What I need or would like to do, is with "users" extended to "records", I would like users to be dynamically updated (however that might be and for whatever reason) but I would like those changes reflected in records. Basically it is a hiearchy.

 

So I want the child to use references from the user table / new class I create.

 

So, the problem I am having is this.

 

class a

{

    public $one;

    public $two;

    public $three;

 

  function __constructor() {}

}

class b extends a {}

 

$a = new user; (will have a list of user variables / properties)

$b = new records; (I want to pass $a to $b)

 

What I want is $b to use a reference from $a, so while in $b I have access to all of the properites of $a which could change dynamically during the life of the program. I would have other classes such as $b that are extentions of $a or child classes but all depend on the $a object.

 

I do not know, maybe instead of creating child classes, I should just pass a $user object into $records without making a child class.

 

 

Working example (previous post for opening information):

 

class a

{

    public $one;

    public $two;

    public $three;

 

  function __constructor() {}

}

 

class b extends a {}

 

$a = new a;

$b = new b;

 

$a->$one = 1;

$a->$two = 2;

 

echo $b->$one;

 

prints ---- "1"

 

What am I missing in my code to make this work.

I still dont think you understand what I've been saying ;)

 

let me try to diagram what's happening internally...

 

- you create a new "a" instance and assign it to $a

 

[pre]

----------------

|      $a      |

| one = null  |

| two = null  |

| three = null |

----------------

[/pre]

 

- you create a new "b" instance and assign it to $b

 

[pre]

-------------------------------

|      $a      |      $b      |

| one = null  | one = null  |

| two = null  | two = null  |

| three = null | three = null |

-------------------------------

[/pre]

 

- you assign 1 to $a->one and 2 to $a->two

 

[pre]

-------------------------------

|      $a      |      $b      |

| one = 1      | one = null  |

| two = 2      | two = null  |

| three = null | three = null |

-------------------------------

[/pre]

 

- you then try to echo $b->one but it's NOT SET, only $a->one is.

 

$b and $a are completely unrelated. The only thing making a class extend another class does is that the child class has all the parent's methods/variables, it does not somehow magically link all instances of a and b together as this would defeat a major purpose of OOP

And I do understand that. So we are on the same page. Now, is it possible through reference or some kind of linkage, to link the instance of $a to $b, passing a reference or what not...

 

Is it possible just to copy $a to $b ($b of course being a child class of $a)

 

And maybe I am missing something in OOP, but it seems like there should be a way through memory sharing, to create and instance of a parent, then create and instance of a child with mirrored or linked data. I can think of allot of reasons to do something like this, especially when the object would be dynamic.

 

Example in previous, say I create a "user" class and then I had a number of other extended classes. There would be no way to create an instance of the extended class without borrowing (sharing) the data from the "user" instance I created?

 

Say part of one of my extended classes was adding extended information for a user and a user instance was already created now I need one of my extended classes. I would have to create a new copy of that data (using more memory and loading time)?

 

Basically, I would like the properties of my parent class referenced into the new child somehow. I understand they are two separate instances, but can the data be linked.

 

Example:

 

class a

{

    pub $one;

    pub $two;

 

    function hello();

    {

            $this->one = 1;

            $this->two = 2;

      }

}

 

class b extends a

{

    function extra_stuff()

    {

          print $this->one;

    }

    function __constuctor($obj)

    {

          $this->one = &$obj->one;

          $this->two = &$obj->two;

    }

}

 

$a = new a;

$a->hello();

$a->one = 3;

 

$b = new b($a);

$b->extra_stuff;

#prints: 3

 

Is there anyway to set the properties of instance b other then how I did it with the constructor? Isn't there a way to do an overlay of some sort or would I have to create a constructor like I did and apply each reference to each matching property?

 

If you see what I am trying to do is to refence the 'a' instance because I want the properties of b to reference a.  In a real world example, lets say instand a would always be a working variable and from time to time I would need instance b, but I need the referenced data of a.

If I understand you correctly, you want something like this:

 

<?php
class A
{
      private $one;

      public function __construct()
      {
      	$this->one = 1;
      }
      public function getOne()
      {		
      	return $this->one;
      }
      
      public function setOne($one)
      {
      	$this->one = $one;
      }
}

class B
{
     private $aInstance;

 public function __construct($a)
     {
	$this->aInstance = $a;
     } 

     public function printOne()
     {
     	echo $this->aInstance->getOne();
     }

}

$a = new A();
$a->setOne(3);

$b1 = new B($a);
$b2 = new B($a);

$b1->printOne();
$b2->printOne();
/ /both print: 3

$a->setOne(5);

$b1->printOne();
$b2->printOne();
// both print: 5

?>

 

Notice that in PHP 5 all objects are passed by reference.

 

Best,

 

Patrick

 

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.