Home > Article > Backend Development > Object-oriented programming in PHP: Methods for developing large-scale PHP projects (1)_PHP Tutorial
limodou
This article introduces object-oriented programming (OOP, Object Oriented Programming) in PHP. I'll show you how to reduce coding and improve quality by using
some OOP concepts and PHP tricks. Good luck!
Concept of object-oriented programming:
Different authors may have different opinions, but an OOP language must have the following aspects:
Abstract data types and information encapsulation
Inheritance
Polymorphism
In PHP, encapsulation is completed through classes:
---------------------------------- -------------------------------------------------- class Something {
// In OOP classes, usually the first character is uppercase
var $x;
function setX($v) {
// Methods start with lowercase words , and then use uppercase letters to separate words, such as getValueOfArea()
$this->x=$v;
}
function getX() {
return $this->x;
}
}
?>---------------------------------------- -------------------------------------
Of course you can follow your own preferences Define it, but it's better to keep it to a standard, which is more effective.
Data members are defined in the class using the "var" declaration. Before the data members are assigned a value, they have no type. A data member
can be an integer, an array, an associative array or an object.
Methods are defined as functions in the class. When accessing class member variables in a method, you should use $this->name, otherwise for a method
it can only be a local variable.
Use the new operator to create an object:
$obj=new Something;