Abstract class a{ abstract public Function B (); } Class B extends a{public function B () { print "OK"; }} $k = new B ();
$k did not call the B () method, why does it appear
There is no problem with the abstract code in the PHP manual, what is wrong with the above code?
Reply to discussion (solution)
Nothing wrong.
Class B extends a{
Public Function B () {
print "OK";
}
}
Method B is a constructor of class B that executes automatically when instantiated, so OK is output
Nothing wrong.
Class B extends a{
Public Function B () {
print "OK";
}
}
Method B is a constructor of class B that executes automatically when instantiated, so OK is output
As long as a function with the same letter as the class name would be considered a constructor?
Nothing wrong.
Class B extends a{
Public Function B () {
print "OK";
}
}
Method B is a constructor of class B that executes automatically when instantiated, so OK is output
Thank you very much, checkout.
That's right!
A method with the same name as a class is a constructor
This is a feature inherited from C + +
And the constructor __construct is inherited from Java.
In PHP, two methods of declaring constructors coexist