0

test.php

class AClass {
    public function __construct()
    {
        echo '<strong style="color:blue;">AClass construct</strong><br>';
    }

    public function call()
    {
        $this->koko();
    }

    private function koko()
    {
        echo 'koko <br>';
    }
}

class BClass extends AClass {

    public function __construct()
    {
        echo '<strong style="color:red;">BClass construct</strong><br>';
        parent::__construct();
    }

    public function momo()
    {
        echo 'momo <br>';
    }
}


$xxx = new AClass(); // Output: AClass contruct ..... (where is BClass echo ?)
$xxx->call(); // Output: koko
$xxx->momo(); // Output: Fatal error: Call to undefined method AClass:momo()

Maybe newbe question but.... What is wrong ?

3
  • 1
    You haven't declared BClass, $xxx is an instance of AClass. Thus, the method doesn't exist.
    – MackieeE
    Commented Jan 20, 2014 at 10:37
  • 1
    You are calling the base class while the function you are trying to call is in inherited class Bclass. Try $xxx = new Bclass()
    – Nouphal.M
    Commented Jan 20, 2014 at 10:39
  • If you are going to use inheritance then be sure to learn how it works. Commented Jan 20, 2014 at 10:39

3 Answers 3

8

You got the wrong direction.. if ClassB extends ClassA, ClassB inherits everything from ClassA and not the other way.. So you have to write the Code as follows:

$xxx = new BClass();
$xxx->call();
$xxx->momo();
3
  • Unfortunately the parent method isn't called, for some reason PHP class has to have its own method even if the one it extends has the method - php 5.5.9
    – NoBugs
    Commented Oct 28, 2014 at 3:45
  • 1
    @NoBugs if you want to call the parent method in an overriden method, you have to do this explicitly with parent::methodName(...)
    – Philipp
    Commented Oct 28, 2014 at 9:39
  • @Philipp Guide me, how can i solve the error - Fatal error: Call to undefined method Mage_Catalog_Model_Resource_Product_Flat::loadAllAttributes() in /home/abc/public_html/app/code/core/Mage/Eav/Model/Entity/Collection/Abstract.php on line 435
    – Gem
    Commented Mar 2, 2018 at 5:13
1

The method (momo) you are trying to access belongs to the child class (BClass) and not to the base class (AClass) and hence the error.

1
$xxx = new BClass();
$xxx->call();
$xxx->momo();

this can call

$aClass = new AClass();
$aClass->call();
// you can't call private method.

if you're not creating the object of class B then you can't call momo()

when you extend a class, the subclass inherits all of the public and protected methods from the parent class. Unless a class overrides those methods, they will retain their original functionality.

Note: Unless autoloading is used, then classes must be defined before they are used. If a class extends another, then the parent class must be declared before the child class structure. This rule applies to classes that inherit other classes and interfaces.

<?php

class foo
{
    public function printItem($string)
    {
        echo 'Foo: ' . $string . PHP_EOL;
    }

    public function printPHP()
    {
        echo 'PHP is great.' . PHP_EOL;
    }
}

class bar extends foo
{
    public function printItem($string)
    {
        echo 'Bar: ' . $string . PHP_EOL;
    }
}

$foo = new foo();
$bar = new bar();
$foo->printItem('baz'); // Output: 'Foo: baz'
$foo->printPHP();       // Output: 'PHP is great' 
$bar->printItem('baz'); // Output: 'Bar: baz'
$bar->printPHP();       // Output: 'PHP is great'

?>

Reference

Not the answer you're looking for? Browse other questions tagged or ask your own question.