PHP Object-Oriented Programming

Source: Internet
Author: User

This article mainly describes PHP object-oriented programming methods. In PHP, we can use classes like C ++ and C # To encapsulate some of the methods we commonly use and then call them when instantiating them.


Constructor:

Class mypc {

Function _ construct (){

Initialization

}

}

 

Destructor:

Class mypc {

......

Function _ destruct (){

......

}

}

 

Encapsulation keywords: public, protected, private

Encapsulate related functions: __set (), _ Get ()

 

_ Set (): Obtain the private attributes encapsulated in the current class or re-execute or assign values to the methods.

 

_ Set ($ name, $ value)

 

_ Get () gets the encapsulated attributes or methods in the current class and converts them to common attributes.

 

_ Get ()

 

Eg:

<? PHP

Class mypc

{

Private
$ Name;

Public
$ Type;

Function
_ Construct ($ name = '')

{

Return
$ This-> name = $ name;

}

 

Private
Function power ()

{

Return
$ This-> name. "power on, starting ...";

}

 

Function _ Get ($ name)

{


Return
$ This-> name ."---";

}

 

Function _ set ($ N, $ V)

{


If ($ v> = 100)


$ This-> $ n = $ V;

}

}

 

$ PC1 = new mypc ('My pc ');

$ PC1-> name = 30;

Echo $ pC1-> name;

?>

 

Inheritance Keyword: extends

 

Base Class Name: Method Name

 

Abstract keywords: Abstract


At least one method in a class is abstract, which is called an abstract class.

Therefore, if an abstract class is defined, the abstract method is first defined.

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.