Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why the constructor is called if the class have a method (function) with his name?

Why does this code echo 'BD'? I am surprised after getting that. I am learning OOP concepts, and new to advanced php.

class A {
    public function a(){
        echo "A";
    }
}
class B extends A {
    public function b(){
        echo "B";
    }
}
class C extends B {
    public function a(){
        echo "C";
    }
}
class D extends C {
    public function a(){
        echo "D";
    }
}

$d = new D();
$d->a();
like image 579
Mihir Vadalia Avatar asked Apr 03 '15 08:04

Mihir Vadalia


People also ask

Why the name of constructor is always same as the class name?

Every class object is created using the same new keyword, so it must have information about the class to which it must create an object. For this reason, the constructor name should be the same as the class name.

What will happen if method name and constructor name are same?

Yes, It is allowed to define a method with the same name as that of a class. There is no compile-time or runtime error will occur. But this is not recommended as per coding standards in Java. Normally the constructor name and class name always the same in Java.

Why constructor is automatically called?

Constructor has same name as the class itself. Default Constructors don't have input argument however, Copy and Parameterized Constructors have input arguments. Constructors don't have return type. A constructor is automatically called when an object is created.

When constructor method is called?

The constructor is called when an object of a class is created. It can be used to set initial values for object attributes. In Java, a constructor is a block of codes similar to the method.

When is the constructor of a class called?

The constructor () method is called automatically when a class is initiated, and it has to have the exact name "constructor", in fact, if you do not have a constructor method, JavaScript will add an invisible and empty constructor method. Note: A class cannot have more than one constructor () method.

When to use a method instead of a constructor?

When for example a string has to be returned from a class that needs to be called then a method could be created, i.e. a constructor will not be sufficient as this will not return anything. In order to explain what I mean, I have created a class with two constructors and two methods that return a string.

What is the use of @constructor in Java?

Constructor looks like method but it is not. It does not have a return type and its name is same as the class name. Mostly it is used to instantiate the instance variables of a class. If the programmer doesn’t write a constructor the compiler writes a constructors on his behalf.

What is the difference between a constructor and a class in JavaScript?

No construct of JavaScript corresponds directly to a Class. But if you call a function with the new keyword in front of it, then that function is called a Constructor, because it constructs and returns a new object. A Class is then simply the set of all objects that were created using the same constructor.


3 Answers

There is an ugly side effect happening. In former version of PHP the constructor of the class must have the same name as the class itself. The name is case-insensitive. Meaning b is the constructor of B in your case. Since D is a child of B and D introduces no own constructor, the constructor of B will get called which triggers the B in output.

In modern versions of PHP the constructor should be called __construct() to avoid such problems. However, you can still use the old mechanism, but it will trigger a notice if your error reporting level is set to E_STRICT.

like image 109
hek2mgl Avatar answered Oct 18 '22 19:10

hek2mgl


When you call $d = new D(); the B constructor is called (because D inherited from C and C inherited from B) and when you call $d->a(); the method a() of your class D is called and print D then you have as output BD.

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.

In your case, your class A and your class B have a constructor

Read Mode at:

http://php.net/manual/en/language.oop5.decon.php

like image 32
Adrian Cid Almaguer Avatar answered Oct 18 '22 19:10

Adrian Cid Almaguer


http://php.net/manual/en/language.oop5.decon.php

If method is named the same as class it is used as a constructor. This is for backwards compatibility with older PHP versions.

like image 43
Mantas Avatar answered Oct 18 '22 19:10

Mantas