First understanding of PHP (3) object-oriented features-how to code singularity

Source: Internet
Author: User
PHP (3) object-oriented features-how to encode the singularity PHP5 began to support object-oriented programming. PHP's object-oriented programming method is not much different from other languages. next we will make a simple record of PHP's basic programming-oriented syntax.

I. object declaration

Declaration method:

Class Say {

Public function SayHello (){

Echo 'Hello World! ';

}

}

This completes the simplest object declaration.

Constructor:

Class Say {

Private $ _ lang;

Public function _ construct ($ lang ){

$ This-> _ lang = $ lang;

Echo 'the language is $ this-> _ lang ';

}

Public function SayHello (){

Echo 'Hello World! ';

}

}

This is how the constructor works.

II. objects used

Call other PHP file methods:

Require 'sayphp ';

However, when this method is called repeatedly, an error of repeated definitions is reported. Therefore, another method is generally used:

Require_once 'sayphp ';

In this way, no error will be reported for repeated calls.

Then a new object is created:

$ A = new Say ('China ');

Use member functions:

$ A-> SayHello ();

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.