无法调试输出

Today i was reading design pattern and i tried to make a sample program which consist of a interface, two class which implement that interface and a main index class.let have a look at the code given below. firstly the interface Iproduct

<?php 
interface Iproduct 
{
    //Define the abstract method
    public function apple();
    public function mango();    
}

the two class which implement the interface

<?php 

// Including the interface
include_once 'Iproduct.php';

    class Apple implements Iproduct 
    {
        public function apple()
        {
            echo ("We sell apples!");
        }   
        public function mango()
        {
            echo ("We do not sell Mango!");
        }
    }
<?php

// Include the interface Iprodduct
include_once 'Iproduct.php';

class Mango implements Iproduct
{
    public function apple()
    {
        echo ("We do not sell Apple");
    } 
    public function mango()
    {
        echo ("We sell mango!");    
    }
}

now the main class

<?php
include_once ('apple.php');
include_once ('Mango.php');

class UserProduct
{
    public function __construct()
    {
        $apple_class_obj=new Apple();
        $mango_class_obj=new Mango();
        //echo("<br/> the apple class object: ".$apple_class_obj);
    }   
}

//creating the object of the UserProduct
echo ("creating the object!<br/>");
$userproduct_obj=new UserProduct();
?>

the output which i get when i execute the code is:

creating the object!
we sell apples!we sell mango

now the problem is that i am unable to get that how is the second output ie, we sell apple! and we sell mango! is being displayed.please let me know the reason

In the past (PHP before version 5), the method with the same name as the class is called when the object is created (PHP old-style constructor methods).

Because PHP is backwards compatible to that behavior, you see the output now.

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. Effectively, it means that the only case that would have compatibility issues is if the class had a method named __construct() which was used for different semantics. [Bold by me]

From: Constructors and Destructors in the PHP Manual

So what you experience is less a problem with the interface or the objects per-se, it's just some side-effect you're likely not aware of (this is really old).

To work around that, just implement a __construct() method in both classes so that the old-style constructor is not called any longer:

class Mango implements Iproduct
{
    public function __construct() {}

    ...

An empty method per class is enough here to stop that.


You might be as well interested in:

in php 4x a method with the same name as the class was considered as the constructor. With php 5x the constructor is explicitly named __construct.

Your experiencing the outcome due to backward compatibility of PHP.