Domanda

I've a code snippet

<?php

abstract class Testing{
    public abstract function tester();

    public function testing(){
        $this->tester();
    }
}

class Test extends Testing{
    public function tester(){
        echo 'test';
    }
}

$t = new Test();
$t->testing();

I'm supposed to have an output test but the output I'm getting is testtest?
Why tester() being called twice?

A reference link to ideone

È stato utile?

Soluzione

PHP scripting language is case - insensitive. (does not apply to variables though)

Since your child class does not have any constructor , the parent class constructor gets fired.

When you do this..

$t = new Test();

The parent class constructor is fired , which is public function testing() , (See the name of the class matches)

From the PHP Docs..

For backwards compatibility, if PHP 5 cannot find a __construct() function for a given class, and the class did not inherit one from a parent class, it will search for the old-style constructor function, by the name of the class.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top