returning a class of the current type



Say I have the following:

<?php
class a {
function copy()
{
return new a();
}
}

class b extends a {
function test()
{
echo 'test';
}
}

$b = new b();
$b = $b->copy();
$b->test();

That'll yield an error because $b, after calling a::copy(), is of type
a - not of type b. Would it be possible to make a::copy() return an
object not of type a but of whatever the top most class is?
.



Relevant Pages

  • Re: php classes calling functions using :: and ->
    ... :: is used for calling a method by class, ... echo "Hello kitty!"; ... It's really not all that rare -- in fact it's quite common. ... available (and PHP will complain accordingly). ...
    (comp.lang.php)
  • Re: returning a class of the current type
    ... class b extends a { ... echo 'test'; ... The correct way to do that would be to have a copymethod in 'b' which calls the copymethod of 'a', then copies its own members. ... Now as matt pointed out, there are workarounds in PHP, but I don't recommend them. ...
    (comp.lang.php)
  • Re: Approving Items
    ... echo $b; // ok, $b was declared global ... // do your stuff, including calling submission_something ... // disconnect once! ...
    (comp.lang.php)
  • Re: newbie question about include() and include_once() functions
    ... Here is my exact code and the error I am receiving when I run this: ... echo "Calling testfunction\n"; ...
    (comp.lang.php)
  • Inheritance of class methods
    ... public function EchoMe ... class B extends A ... and i should get echo "test" on screen. ... PHP 5.2.4 ...
    (php.general)