There is no problem with the abstract code in the PHP manual, what is wrong with the following code?

Source: Internet
Author: User
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

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.