This article focuses on PHP object-oriented programming (OOP ). I will demonstrate how to use object-oriented concepts to compile less code but better programs. Good luck. The concept of object-oriented programming has different opinions for every author. I suggest This article focuses on PHP object-oriented programming (OOP ). I will demonstrate how to use object-oriented concepts to compile less code but better programs. Good luck.
The concept of object-oriented programming has different opinions for each author. I suggest the following:
-Data abstraction and information hiding
-Continuous
-Polymorphism
Measure to encapsulate the application class in PHP:
Class Something {
// In OOP classes are usually named starting with a cap letter.
Var $ x;
Function setX ($ v ){
// Methods start in lowercase then use lowercase to seprate
// Words in the method name example getValueOfArea ()
$ This-> x = $ v;
}
Function getX (){
Return $ this-> x;
}
}
?>
Of course you can use your own measures, but there is always a good scale.
The data member of the class in PHP applies the 'var' definition, and the data member has no type until it is assigned a value. A data member may be an integer, array, associative array, or even an object ). A method is defined as a function in a class. to access data members in a method, you must apply $ this-> name. Otherwise, a method is a local variable of a function.
Apply new to create an object
$ Obj = new Something;
Then apply the member function
$ Obj-> setX (5 );
$ See = $ obj-> getX ();
The setX member function assigns 5 to the member variables in the object (rather than the class) obj, and then returns the value of getX 5.
You can also use object references to access member variables, such as $ obj-> x = 6. However, this is not a good object-oriented programming method. I keep you applying the member function to set the value of the member variable and reading the member variable through the member function. If you think that member variables are unaccessable except for the application member functions, you will become a good object-oriented programmer. Unfortunately, PHP has no way to declare that a variable is private, so it promises that bad code exists.
Extend is continuously applied to declare in PHP.
Class Another extends Something {
Var $ y;
Function setY ($ v ){
// Methods start in lowercase then use lowercase to seperate
// Words in the method name example getValueOfArea ()
$ This-> y = $ v;
}
Function getY (){
Return $ this-> y;
}
}
?>
In this way, the object of the 'Another 'class has the member variables and method functions used by the parent class, plus its own member variables and member functions. For example:
$ Obj2 = new Another;
$ Obj2-> setX (6 );
$ Obj2-> setY (7 );
Multi-class persistence is not supported, so you cannot make a class continuous with multiple classes.
In the continuous class, you can redefine the method. if we redefine getX in 'another ', we will no longer be able to access the member function getX in 'something. similarly, if we declare a member variable with the same name as the parent class in the continuous class, the variable of the continuous class will hide the variable with the same name as the parent class.
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:
[email protected]
and provide relevant evidence. A staff member will contact you within 5 working days.