0

I have 2 classes that one of them extends another:

class a{
    private $name;
    public function __construct(){

    }
    public function get($v){
        $this->name=$v;
    }
}
class b extends a{
    public $user;
}

at top of page I'm creating an object:

$a=new a();
$a->get(1234);

and then I'm creating another object from class b.

$b=new b();

but I want to copy all current variables of $a to $b. so class $b would have variable $name with value 1234.

how can I do this?

Hossein
  • 13
  • 4
  • Class b depends on class a. So class b should have a method that takes a as an input. If you want to copy all properties (I question the sanity of this) look at [`get_object_vars()`](http://php.net/get_object_vars) – Mike B Dec 17 '13 at 18:04
  • all "class a" variables exists on "class b". I just to copy object $a current variables status to $b. and by get_object_vars I can not get private ones. and because of security I don't want to change object type! – Hossein Dec 17 '13 at 18:14
  • You wouldn't be able to read `$name` in the child class unless you have a getter, as `$name` is private. – h2ooooooo Dec 17 '13 at 18:17
  • 1
    This is the opposite of OOP. A child object should only be aware of a parent via inheritance. Passing a parent into a child is all kinds of bad design. http://en.wikipedia.org/wiki/Liskov_substitution_principle. You have a Car object and an extending Truck object. For some reason you're saying you have to instantiate a car, add the wheels, then pass that into Truck for it to know how many wheels it has. Why not just add that functionality to the base (Car) and add the wheels to the Truck? – Mike B Dec 17 '13 at 18:17

3 Answers3

1

There are good reasons not to do this..

However - some hacks with serialize/deserialize could make an instance of class a class b

function copyDown($obj, $newInstance) {
    $data = serialize($obj);
    $newData = preg_replace('~(^O:[0-9]+:)"([^"]+")~', '$1"' . $newInstance . '"', $data);
    return unserialize($newData);
}

$a = new ClassA();
$b = copyDown($a, 'ClassB');

But be aware that ClassB should inherit from ClassA

Philipp
  • 15,377
  • 4
  • 35
  • 52
  • [Here's a demo of this working](https://eval.in/80405). OP: Seriously - there *are* good reasons not to do this though, so please.. *think*. – h2ooooooo Dec 17 '13 at 18:35
  • why? as long as you don't mess up your code, this little "cast" works perfectly - maybe it's not that fast.. btw: http://www.adaniels.nl/articles/a-dark-corner-of-php-class-casting/comment-page-1/ – Philipp Dec 17 '13 at 18:45
  • *There are good reasons not to do this..* Please describe them, we can't guess what you're thinking. – A.L Dec 19 '13 at 16:41
  • I.E. you could cast practically to anything and the stuff, the constructor have to do, isn't done(if it's dont handled in the __wakeup method). This could result in an instable state of the class. – Philipp Dec 21 '13 at 13:55
0

Add a constructor to b that takes a parameter of type a?

class b extends a{
    public $user;

    function __construct(a $a)
    {
         parent::__construct($a.name);
    }
}
afuzzyllama
  • 6,538
  • 5
  • 47
  • 64
  • I think it'll work! but I'm not familiar with objects. can you do it by another method like copyVars()? this way will work for private variables? thanx :) – Hossein Dec 17 '13 at 18:18
  • Instead of using the `private` keyword, use the `protected` keyword. This way all children or classes extended from `a` will be able to access that member. – afuzzyllama Dec 17 '13 at 18:23
  • sorry, but is there any way to copy object with all variables at once by one command. for example in above code in construct method we write something like $this=$a;... – Hossein Dec 17 '13 at 18:50
  • Just make a copy constructor for `a`? The code is exactly the same as what is above. Just copy each value to the member. – afuzzyllama Dec 17 '13 at 19:03
0

I think if you create object of class b by passing the value then you can access the variables of class a in class b as class b is extending class a.

<?php
class a {
    private $name;

    public function __construct($v){
        $this->name=$v;
    }
}
class b extends a {
    public $user;
}

$b = new b(1234);

var_dump($b);
?>