Home  >  Article  >  Backend Development  >  Object-oriented programming in PHP: Methods for developing large-scale PHP projects (1)_PHP Tutorial

Object-oriented programming in PHP: Methods for developing large-scale PHP projects (1)_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:22:31985browse

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;


Then you can use member functions via:
$obj->setX(5);
$see=$obj->getX();
In this example, the setX member function assigns 5 to the object’s member variable x (not the class), and then getX returns its value 5.
You can access data members through class references like: $obj->x=6. This is not a good OOP habit. I strongly recommend accessing member variables through
methods. If you treat member variables as unmanipulable, and only use methods through object handles, you will be a good OOP programmer. Unfortunately, PHP does not support declaring private member variables, so bad code is allowed in PHP.
Inheritance is easy to implement in PHP, just use the extend keyword.
----------------------------------------------- ----------------------------------
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'll show you how to reduce coding by using some OOP concepts and PHP tricks...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn