There is no problem with the abstract code in the php Manual. what is wrong with the following code?

Source: Internet
Author: User
There is no problem with the abstract code in the php Manual. what is wrong with the following code?
abstract class A{    abstract public function b();   }class B extends A{    public function b(){        print "ok";    }}$k = new B();

Why does $ k not call the B () method?
There is no problem with the abstract code in the php Manual. where is the above code wrong?


Reply to discussion (solution)

No error

Class B extends {
Public function B (){
Print "OK ";
}
}
Method B is a constructor of Class B. it is automatically executed during instantiation, so OK is output.

No error

Class B extends {
Public function B (){
Print "OK ";
}
}
Method B is a constructor of Class B. it is automatically executed during instantiation, so OK is output.


Will a function with the same letter as the class name be treated as a constructor?

No error

Class B extends {
Public function B (){
Print "OK ";
}
}
Method B is a constructor of Class B. it is automatically executed during instantiation, so OK is output.


Thank you very much.

Yes!
The method with the same name as the class is the constructor.
This is a feature inherited from C ++.

Construct is inherited from Java.

In php, the two methods for declaring constructor 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.