I am surprised for why the constructor is called when we have different class and constructor name. Constructor name is starting with small "r"?
class Registration{
function registratio开发者_JAVA技巧n(){
echo "Constructor is called.";
}
}
$obj = new Registration();
//$obj->registration();
Outputs: Constructor is called.
Modification: Does this case-insensitive behavior depends on php versions we are using?
php is case-insensitive (sometimes). The following would work as well:
CLASS REGISTRATION {
FUNCTION reGISTration(){
ECHO "constructor is called.";
}
}
$obj = NEW Registration();
In php, all function names are case-insensitive.
By the way, you should switch to the new-style __construct
. Constructors as functions with the name of the class are a historical artifact.
I think both names are same ..
because when you try to declare the class with name "registration" in same page, it will give you an error that states that you can not re declare the class..
in this case , it is not case sensitive
PHP is case insensitive, but this doesn't explain the behaviour.
This behaviour is because a function with the same name of the class is treated as the constructor.
See http://php.net/manual/en/language.oop5.decon.php - Example 2
So this is true for functions of any given name, EG:
class Dog{
function dog(){
echo "Constructor is called.";
}
}
$obj = new Dog();
$obj->dog();
精彩评论