Home  >  Article  >  Backend Development  >  PHP study notes: object-oriented design_PHP tutorial

PHP study notes: object-oriented design_PHP tutorial

WBOY
WBOYOriginal
2016-07-20 11:17:13716browse

Object-oriented design is a very important content block in PHP program development. If you want to become a senior PHP programmer, we must know the specific usage and writing of object-oriented design.

Simple maintenance Modularity is a feature of object-oriented programming. Entities are represented as classes and classes with the same functionality in the same namespace, we can add a class in a namespace without affecting other members of the namespace.

Extensibility Object-oriented programming inherently supports extensibility. If you have a class with a certain function, you can quickly extend this class and create a class with expanded functions.

Code reuse Since functions are encapsulated in classes, and classes exist as independent entities, it is very simple to provide a class library.

It is more suitable for multiple people to cooperate to develop projects, so many large and medium-sized websites now choose to use OOP for development.

This article mainly explains the most basic methods and code examples of object-oriented programming using PHP, how to create a class and how to generate an instance of a class, etc. It is just an introduction, very simple, if you want to learn this in depth is far from enough. Only suitable for php beginners

Public means global, accessible to both internal and external subclasses of the class;

代码如下 复制代码

class Test{
public $name='Janking',
$sex='male',
$age=23;

function __construct(){
echo $this->age.'
'.$this->name.'
'.$this->sex.'
';
}

function func(){
echo $this->age.'
'.$this->name.'
'.$this->sex.'
';
}
}


$P=new Test();
echo '

';
$P->age=100;
$P->name="Rainy";
$P->sex="female";
$P->func();
?>

  private表示私有的,只有本类内部可以使用;

代码如下 复制代码



class Test{
private $name='Janking',
$sex='male',
$age=23;

function __construct(){
$this->funcOne();
}

function func(){
echo $this->age.'
'.$this->name.'
'.$this->sex.'
';
}

private function funcOne(){
echo $this->age.'
'.$this->name.'
'.$this->sex.'
';
}
}


$P=new Test();
echo '

';
$P->func();
$P->age=100; // Cannot access private property Test::$age
$P->name="Rainy"; // Cannot access private property Test::$name
$P->sex="female"; // Cannot access private property Test::$female
$P->funcOne(); // Call to private method Test::funcOne() from context ''
?>

代码如下 复制代码



class Test{
private $name='Janking',
$sex='male',
$age=23;

function __construct(){
$this->funcOne();
}

function func(){
echo $this->age.'
'.$this->name.'
'.$this->sex.'
';
}

private function funcOne(){
echo $this->age.'
'.$this->name.'
'.$this->sex.'
';
}
}


$P=new Test();
echo '

';
$P->func();
$P->age=100; // Cannot access private property Test::$age
$P->name="Rainy"; // Cannot access private property Test::$name
$P->sex="female"; // Cannot access private property Test::$female
$P->funcOne(); // Call to private method Test::funcOne() from context ''
?>

  protected表示受保护的,只有本类或子类或父类中可以访问;

  - 数据抽象和信息隐藏

  - 继承

  - 多态性

  在PHP中使用类进行封装的办法:

代码如下 复制代码

class Something {
// In OOP classes are usually named starting with a cap letter.
var $x;

代码如下 复制代码

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;
}
}

?>

  

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 method, but it’s always good to have a standard.

Data members of classes in PHP are defined using "var". Data members have no type until they are assigned a value. A data member may be an integer, an array, an associative array (associative array) or even an object. Methods are defined as functions in the class. To access data members in the method, you must use $this->name like this method, otherwise it is a local variable of a function for the method.

Use new to create an object

The code is as follows Copy code

$obj = new Something;

代码如下 复制代码

$obj = new Something;

Then use the member function

The code is as follows Copy code

$obj->setX(5); $see = $obj->getX();

代码如下 复制代码

$obj->setX(5);
$see = $obj->getX();

 The setX member function assigns 5 to the member variable in the object (not the class) obj, and then getX returns the value 5.

You can also use object references to access member variables, for example: $obj->x=6; However, this is not a good object-oriented programming method. I insist that you use member functions to set the value of member variables and member functions to read member variables. If you believe that member variables are not accessible except by using member functions, you will become a good object-oriented programmer. But unfortunately PHP itself has no way to declare a variable as private, so bad code is allowed to exist.

Inheritance is declared using extend in PHP.

The code is as follows Copy code

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;
}

代码如下 复制代码

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;
}
}

?>

  

function getY() {
return $this->y;
}
}

?>

 

In this way, the object of class "Another" has all the member variables and method functions of the parent class, plus its own member variables and member functions. Such as:

The code is as follows Copy code

$obj2=new Another;
$obj2->setX(6);
$obj2->setY(7);

代码如下 复制代码

$obj2=new Another;
$obj2->setX(6);
$obj2->setY(7);

Multiple inheritance is not supported, so you cannot have a class inherit from multiple classes.

In an inherited class, you can redefine it to redefine the method. If we redefine getX in "Another", then we can no longer access the member function getX in "Something". Similarly, if we redefine it in an inherited class Declare a member variable with the same name as the parent class, then the variable of the inherited class will hide the variable of the same name of the parent class.

You can define a constructor of a class. The constructor is a member function with the same name as the class and is called when you create an object of the class.

The code is as follows Copy code

class Something {
var $x;

代码如下 复制代码

class Something {
var $x;

 function Something($y) {
$this->x=$y;
}

 function setX($v) {
$this->x=$v;
}

 function getX() {
return $this->x;
}
}

?>

  

function Something($y) {
$this->x=$y;
}

function setX($v) {
$this->x=$v;
}

function getX() {
return $this->x;
}
}

代码如下 复制代码

$obj=new Something(6);

?>

 

So you can create objects using the following method:
The code is as follows Copy code

$obj=new Something(6);

Constructor automatically assigns value 5 to member variable x. Constructor and member function are ordinary PHP functions, so you can use default parameters.

The code is as follows Copy code

function Something($x="3", $y="5")

代码如下 复制代码

function Something($x="3",$y="5")

Then:

The code is as follows Copy code

$obj=new Something(); // x=3 and y=5
$obj=new Something(8); // x=8 and y=5
$obj=new Something(8,9); // x=8 and y=9

代码如下 复制代码

$obj=new Something(); // x=3 and y=5
$obj=new Something(8); // x=8 and y=5
$obj=new Something(8,9); // x=8 and y=9

Default parameters are defined in the same way as C++, so you cannot pass a value to Y but let X take the default value. The actual parameters are passed from left to right. When there are no more actual parameters, the function will use Default parameters.

Only when the constructor of the inherited class is called, the object of the inherited class is created, and the constructor of the parent class is not called. This is a feature of PHP that is different from other object-oriented languages, because the constructor call chain is object-oriented. Characteristics of programming. If you want to call the base class's constructor, you have to call it explicitly in the inheriting class's constructor. This works because all methods of the parent class are available in the inherited class.

The code is as follows Copy code

function Another() {
$this->y=5;
$this->Something(); //explicit call to base class constructor.
}

代码如下 复制代码

function Another() {
$this->y=5;
$this->Something(); //explicit call to base class constructor.
}

  ?>

  多态性。

代码如下 复制代码

function niceDrawing($x) {
//Supose this is a method of the class Board.
$x->draw();
}

代码如下 复制代码

function niceDrawing($x) {
//Supose this is a method of the class Board.
$x->draw();
}

$obj=new Circle(3,187);
$obj2=new Rectangle(4,5);

$board->niceDrawing($obj); //will call the draw method of Circle.
$board->niceDrawing($obj2); //will call the draw method of Rectangle.

$obj=new Circle(3,187);
$obj2=new Rectangle(4,5);

$board->niceDrawing($obj); //will call the draw method of Circle.
$board->niceDrawing($obj2); //will call the draw method of Rectangle.

  ?>

  和封装有关的魔术方法:

  __set():是直接设置私有成员属性值时,自动调用的方法

  __get():是直接获取私有成员属性值时,自动调用的方法

  __isset(); 是直接isset查看对象中私有属性是否存时自动调用这个方法

  __unset(); 是直接unset删除对象中私有属性时,自动调用的方法

http://www.bkjia.com/PHPjc/372033.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/372033.htmlTechArticle
面向对象设计是php程序开发中一个很重要的内容块,如果你想成为高级php程序员我们必须知道面向对象设计具体用法与写法。 维护简单 模...
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