PHP object-oriented programming: method for developing large PHP projects (1) _ PHP Tutorial

Source: Internet
Author: User
PHP object-oriented programming: method for developing large PHP projects (1 ). Limodou This article introduces object-oriented programming (OOP, ObjectOrientedProgramming) in PHP ). I will show you how to reduce coding limodou by using some OOP concepts and PHP skills
This article introduces object-oriented Programming (OOP) in PHP ). I will show you how to connect
Using some OOP concepts and PHP skills to reduce coding and improve quality. Good luck!
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:
-------------------------------------------------------------------------------- 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
It is an integer, an array, an associated array, or an object.
The method is defined as a function in the class. when using a member variable of the class in the method, you should use $ this-> name. otherwise
It 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.
You can access data members by referencing classes like $ obj-> x = 6. this is not a good OOP habit. I strongly recommend
Methods to access member variables. If you think of the member variables as unprocessable and use the methods only through the object handle, 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.
--------------------------------------------------------------------------------
Class Another extends Something {
Var $ y;
Function setY ($ v ){
$ This-> y = $ v;
}
Function getY (){
Return $ this-> y;
}
}
?> --------------------------------------------------------------------------------
Next page

Http://www.bkjia.com/PHPjc/532305.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/532305.htmlTechArticlelimodou This article introduces object-oriented Programming (OOP, Object Oriented Programming) in PHP ). I will show you how to reduce coding by using some OOP concepts and PHP skills...

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.