PHP object-oriented programming

Source: Internet
Author: User
Tags inheritance
The concept of object-oriented programming:
Different authors may have different ideas, but an OOP language must have the following aspects:
Abstract data types and information encapsulation
Inheritance
Polymorphism
In PHP, classes are encapsulated:
<? Php
Class Something {
// In The OOP class, the first character is usually uppercase.
Var $ x;
Function setX ($ v ){
// The method starts with lowercase words, and then uses uppercase letters to separate words, such as getValueOfArea ()
$ This-> x = $ v;
}
Function getX (){
Return $ this-> x;
}
}
Of course, you can define it according to your preferences, but it is better to maintain a standard to make it more effective. Data members are defined using the "var" declaration in the class. They have no type before assigning values to data members. A data member can be an integer, an array, an associated array, or an object. A method is defined as a function in a class. When using a member variable of the class in a method, you should use $ this-> name. Otherwise, a method can only be a local variable.
Use the new operator to create an object:
$ Obj = new Something;
Then you can use the member function to pass:
$ Obj-> setX (5 );
$ See = $ obj-> getX ();
In this example, the setX member function assigns 5 to the object's member variable x (not a class), and then getX returns its value 5. It is not a good OOP habit to access data members by referencing classes like $ obj-> x = 6. I strongly recommend that you use methods to access member variables. If you think of member variables as unmanageable and use methods only through object handles, you will be a good OOP programmer. Unfortunately, PHP does not support declaring private member variables, so bad code is also allowed in PHP. Inheritance is easy to implement in PHP, as long as the extend keyword is used.
<? Php
Class Another extends Something {
Var $ y;
Function setY ($ v ){
$ This-> y = $ v;
}
Function getY (){
Return $ this-> y;
}
}
The object of the "Another" class now has all the data members and methods of the parent class (Something), and adds its own data members and methods.
You can use
$ Obj2 = new Something;
$ Obj2-> setX (6 );
$ Obj2-> setY (7 );
PHP currently does not support multi-inheritance, so you cannot derive a new class from two or more classes. You can redefine a method in a derived class. If we redefine the getX method in the "Another" class, we cannot use the getX method in "Something. If you declare a data member with the same name as the base class in the derived class, it will "hide" the data member of the base class when you process it.

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.