Why is my constructor still calling, even if the case of the class and constructor is different?

I am surprised why the constructor is called when we have a different class and constructor name. Does the constructor name begin with a small "r"?

class Registration{ function registration(){ echo "Constructor is called."; } } $obj = new Registration(); //$obj->registration(); 

Outputs: The constructor is called.

Modification: Does this case insensitive behavior depend on the php versions we use?

+4
source share
4 answers

php is not case sensitive (sometimes). The following will work:

 CLASS REGISTRATION { FUNCTION reGISTration(){ ECHO "constructor is called."; } } $obj = NEW Registration(); 
+7
source

In php, all function names are case insensitive .

By the way, you should switch to the new __construct style . Constructors as functions with a class name are a historical artifact.

+7
source

I think both names are the same.

because when you try to declare a class with the name "register" on the same page, it will give you an error message indicating that you cannot declare the class.

in this case it is not case sensitive

0
source

PHP is case insensitive, but this does not explain the behavior.

This behavior is due to the fact that a function with the same class name is considered as a constructor.

See http://php.net/manual/en/language.oop5.decon.php - Example 2

So, this is true for functions of any name, eg:

 class Dog{ function dog(){ echo "Constructor is called."; } } $obj = new Dog(); $obj->dog(); 
0
source

All Articles