1

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

jh314
  • 27,144
  • 16
  • 62
  • 82
Anurag Singh
  • 727
  • 6
  • 10
  • 27

2 Answers2

4

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:

Community
  • 1
  • 1
hakre
  • 193,403
  • 52
  • 435
  • 836
  • thanks for that so in order to prevent it what i should do...means what changes i should make – Anurag Singh Aug 08 '13 at 13:40
  • I added you some simple example and description how you can easily work around that. Let me know if it's now more clear. – hakre Aug 08 '13 at 13:40
  • i got all what you explained thanks a lot...as i am switching to oops concept so...i am new to it.thanks – Anurag Singh Aug 08 '13 at 13:48
  • No problem, if you didn't know what a *constructor* is so far, learn about it as well. It's a common concept in OOP, not specific to OOP in PHP. – hakre Aug 08 '13 at 13:49
0

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.

DevZer0
  • 13,433
  • 7
  • 27
  • 51