# Method inheritance and parent classes

- Thread: https://externals.io/message/25200
- Original archive: https://news-web.php.net/php.internals/25200
- Started by Christian Weiske on 2006-08-04T16:46:28+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #25200 — Christian Weiske — 2006-08-04T16:46:28+00:00

URL: https://externals.io/message/25200#25200

Hello all,


I recently had a problem that could be solved, but in an ugly way:
Class B extended Class A. Both classes define a method with the same
name. B called A's constructor, and A called $this->method(). This
executed B::method(), not A::method() as you would expect it.

<?php
class A {
    function __construct() {
        $this->build();
    }

    function build() {
        echo "buildA ";
    }
}

class B extends A {
    function __construct() {
        parent::__construct();
        $this->build();
    }

    function build() {
        echo "buildB ";
    }
}
new B();
?>

output is: "buildB buildB "
expected would be: "buildA buildB "

I suggest changing the behavior of php to never call methods of classes
that are added by subclasses, only own ones or inherited ones.

-- 
Regards/MfG,
Christian Weiske

---

## #25201 — Jason Sweat — 2006-08-04T17:15:47+00:00

URL: https://externals.io/message/25200#25201 · In reply to: #25200

> I suggest changing the behavior of php to never call methods of classes
> that are added by subclasses, only own ones or inherited ones.

Thus rendering the entire Template Method design pattern useless?

http://home.earthlink.net/~huston2/dp/templateMethod.html


-- 
Regards,
Jason
http://blog.casey-sweat.us/

---

## #25202 — Richard Lynch — 2006-08-04T19:59:38+00:00

URL: https://externals.io/message/25200#25202 · In reply to: #25200

> I recently had a problem that could be solved, but in an ugly way:
> Class B extended Class A. Both classes define a method with the same
> name. B called A's constructor, and A called $this->method(). This
> executed B::method(), not A::method() as you would expect it.

Most OOP people expect it to run B::method()

That's rather the whole point of inheritence and subclasses.

If you need to override it, you might be able to use A::method()...
Though the OO purists have made that illegal now, I think. :-;

There may be some kind of get_method() function on a class that you
could use like:
$function = get_method('A', 'method');
user_call_func(array($this, $function));

-- 
Like Music?
http://l-i-e.com/artists.htm

---

## #25204 — Marian Kostadinov — 2006-08-05T06:12:59+00:00

URL: https://externals.io/message/25200#25204 · In reply to: #25200

use self::build() or A::build() for that case

2006/8/4, Christian Weiske <cweiske@cweiske.de>:
>
> Hello all,
>
>
> I recently had a problem that could be solved, but in an ugly way:
> Class B extended Class A. Both classes define a method with the same
> name. B called A's constructor, and A called $this->method(). This
> executed B::method(), not A::method() as you would expect it.
>
> <?php
> class A {
>     function __construct() {
>         $this->build();
>     }
>
>     function build() {
>         echo "buildA ";
>     }
> }
>
> class B extends A {
>     function __construct() {
>         parent::__construct();
>         $this->build();
>     }
>
>     function build() {
>         echo "buildB ";
>     }
> }
> new B();
> ?>
>
> output is: "buildB buildB "
> expected would be: "buildA buildB "
>
> I suggest changing the behavior of php to never call methods of classes
> that are added by subclasses, only own ones or inherited ones.
>
> --
> Regards/MfG,
> Christian Weiske
