Limodou This article introduces object-oriented programming (OOP, ObjectOrientedProgramming) in PHP ). I will show you how to reduce coding and improve quality by using some OOP concepts and PHP skills. Good luck! Object-oriented programming: different authors of SyntaxHighlighter
Limodou
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