Home >Backend Development >PHP Tutorial >PHP object-oriented development study notes_PHP tutorial

PHP object-oriented development study notes_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:50:13864browse

Object-oriented programming (English: Object-oriented programming, abbreviation: OOP) refers to a programming paradigm and is also a method of program development. Object refers to a collection of classes. It takes objects as the basic unit of the program and encapsulates programs and data to improve the reusability, flexibility and scalability of the software

Note:
1. When defining an object method, although there is no need to write public in front of it, the default is public method, but it is recommended to write it.

First day of object-oriented php

1. What is object-oriented ?


Elements: abstraction, encapsulation, sharing, emphasizing object structure rather than program structure.

What is a class?
It can be understood as a function collection menu, and we use classes to generate our methods.
Example: a computer
Class: Collection (encapsulation) of display/keyboard/host...
Object: monitor/keyboard/host... one of them, has its own unique function.
Properties: computer.
Methods: Improve functions, watch movies, play games, program, surf the Internet...

2. Use class to create a class
Only with classes can we have methods.
Format:
class method name {
......
}

Member attribute: Custom variable (just a name).
Member methods: custom functions.

3. Member attributes and member methods of the class
Note: Use public instead of var to define member properties.

Example:

The code is as follows Copy code
class MyPc { //Declare a class, define A method MyPc.
 代码如下 复制代码
class MyPc { //声明一个类,定义一个方法MyPc。
public $name; //成员属性。
var $price; //成员属性
function vod() { //成员方法,实现返回字符串功能。
return "test";
}
}
public $name; //Member properties.

var $price; //Member attributes
function vod() { //Member method to implement the function of returning a string.
return "test";
}

}
 代码如下 复制代码

class MyPc { //声明一个类,定义一个方法MyPc。
public $name; //成员属性。
var $price; //成员属性
function vod() { //成员方法,实现返回字符串功能。
return "test";
}
}

$pc1 = new Mypc(); //实例化
$pc1 -> name;

4. Use new function object to instantiate
Format: new object name (parameter)

Example 1:
 代码如下 复制代码

class MyPc {

var $key;
public $name;

function vod() {
echo "hello";
}

}

$pc1 = new MyPc();
$pc1->key = "10101010";

echo $pc1->key;
?>


 

The code is as follows Copy code
class MyPc { //Declare a class and define a method MyPc. public $name; //Member properties. var $price; //Member attributes function vod() { //Member method to implement the function of returning a string. return "test"; } } $pc1 = new Mypc(); //Instantiation $pc1 -> name;
Example 2:
The code is as follows Copy code
class MyPc {<🎜> <🎜> var $key;<🎜> public $name;<🎜> <🎜> function vod() {<🎜> echo "hello";<🎜> }<🎜> <🎜>}<🎜> <🎜>$pc1 = new MyPc();<🎜> $pc1->key = "10101010"; echo $pc1->key; ?>

php object-oriented second day


1. Create one and multiple objects.
2. The $this keyword in the object.
3. Initialize the object __construct()
4. Destructor __destruct()

----------------------------------------

1. Create one and multiple objects
To create one or more objects we only need to use the new function multiple times to instantiate the class.
Example:

The code is as follows Copy code
 代码如下 复制代码

class MyPc {
public $name;
public $price;

function vod() {
return "播放电影";
}
....
}

$pc1 = new MyPc();
$pc2 = new MyPc();
$pc3 = new MyPc();

 

class MyPc {

public $name;
public $price;

function vod() {

return "Play movie";
 代码如下 复制代码
class MyPc {
public $name;--------
public $price;      |
                    |
function vod() {    |
$this->name;<--------
}
....
$this->vod(); //这样输出调用的就是 public $name;
....
}
}

....

}
 代码如下 复制代码

class MyPc {
public $name;

function vod() {
return $this->name . " 正在播放 未来战警";
 }
 function game() {
  return $this->vod() . " 正在运行 魔兽世界";
 }

}
$pc1 = new MyPc();
$pc2 = new MyPc();

$pc1->name = "1号计算机";
$pc2->name = "2号计算机";
echo $pc1->vod() . "
";
echo $pc2->game() . "
";
?>


 

$pc1 = new MyPc();
$pc2 = new MyPc();

$pc3 = new MyPc();




2. $this keyword in the object
The $this keyword is a system variable used to access object properties and object methods in the current object.
Scope: Within this category. Example:

The code is as follows Copy code
class MyPc { public $name;--------
 代码如下 复制代码

class MyPc {
public $name;

function __construct($name = "") { //初始化。
$this->name = $name; //把初始化内容第四行的 $name = "" 赋值给 $this 取到的第三行的 public $name;。
 }

 function vod() {
  return $this->name . " 正在播放 未来战警";
 }

 function game() {
  return $this->vod() . " 正在运行 魔兽世界";
 }

}
$pc1 = new MyPc("1号计算机"); //这里进行初始化。
$pc2 = new MyPc("2号计算机");

echo $pc1->vod() . "
";
echo $pc2->game() . "
";
?>

public $price; |                                       function vod() { | $this->name;<--------<🎜> }<🎜> ....<🎜> $this->vod(); //The output call is public $name; .... }
Example 2:
The code is as follows Copy code
class MyPc {<🎜> public $name;<🎜> <🎜> function vod() {<🎜> Return $this->name . "Now playing Futuremen"; } function game() { Return $this->vod() . "Running World of Warcraft"; } } $pc1 = new MyPc(); $pc2 = new MyPc(); $pc1->name = "Computer No. 1"; $pc2->name = "Computer No. 2"; echo $pc1->vod() . "
"; echo $pc2->game() . "
"; ?>
3.Initialize object __construct() Initialization is equivalent to pre-booking the initial value of a member attribute. Format: class MyPc { function __construct() { Initialization operation } } Example:
The code is as follows Copy code
<🎜>class MyPc {<🎜> public $name;<🎜> <🎜> function __construct($name = "") { //Initialization. <🎜> $this->name = $name; //Assign $name = "" in the fourth line of the initialization content to public $name; in the third line obtained by $this. } function vod() { Return $this->name . "Now playing Futuremen"; } function game() { Return $this->vod() . "Running World of Warcraft"; } } $pc1 = new MyPc("Computer No. 1"); //Initialization is done here. $pc2 = new MyPc("Computer No. 2"); echo $pc1->vod() . "
"; echo $pc2->game() . "
"; ?>

4. Destructor __destruct()
Methods that can be automatically called when an object is released are called destructors and can also be understood as garbage collection mechanisms.
Rules: Last in, first out, instantiated first and then released, last instantiated and called, first released.
is called last.

Example:

The code is as follows Copy code
 代码如下 复制代码

class MyPc {
public $name;

function __construct($name = "") {
$this->name = $name;
 }

 function vod() {
  return $this->name . " 正在播放 未来战警";
 }

 function game() {
  return $this->vod() . " 正在运行 魔兽世界";
 }

 function __destruct() { //后进先出。
  echo "
垃圾回收机制:" . $this->name . "
";
 }

}
$pc1 = new MyPc("1号计算机");
$pc2 = new MyPc("2号计算机");

echo $pc1->vod() . "
";
// $pc1 = null; //这里是当特殊需要执行完毕立即回收内存的话,可以使用null。
echo $pc2->game() . "
";
?>

class MyPc { public $name; function __construct($name = "") {

$this->name = $name;

}
function vod() { Return $this->name . "Now playing Futuremen";

}


function game() {

Return $this->vod() . "Running World of Warcraft";

}

function __destruct() { //Last in, first out.
echo "
Garbage collection mechanism:" . $this->name . "
";

}


}
$pc1 = new MyPc("Computer No. 1");
$pc2 = new MyPc("Computer No. 2");

echo $pc1->vod() . "
";
// $pc1 = null; //Here, if the memory is immediately reclaimed when the special need is executed, null can be used.
echo $pc2->game() . "
";
?>

 代码如下 复制代码

class MyPc {
public $name; //公共属性
protected $price; //保护属性

private function vod() { //私有属性
$this->name;
 }
  . . .
 }
?>

PHP object-oriented third day Class encapsulation and application 1. Encapsulation keywords: public, protected, private. 2. Encapsulate related functions: __set(), __get(). -------------------------------------------------- 1. Encapsulation keywords: public, protected, private Encapsulation is to hide some related attributes and behaviors to achieve protection and security. Encapsulation keyword Public: Indicates that it is global and can be accessed by subclasses inside and outside the class. protected: Indicates that it is protected and can only be accessed in this class or subclass or parent class. private: means private and can only be used within this class. [Important: When this keyword is used, it calls private properties or methods, which can only be called in this class. It feels like one more process and a springboard. See Example 2 for details] public protected private Overall 1 0 0 Inherited class 1 1 1 0 This Category 1 1 1 Example:
The code is as follows Copy code
class MyPc {<🎜> public $name; //Public attribute<🎜> protected $price; //Protected attribute<🎜> <🎜> private function vod() { //Private attribute<🎜> $this->name; } . . . } ?>

Example 2:

The code is as follows Copy code
 代码如下 复制代码

class MyPc {
private $name;
function __construct($name = "") {
return $this->name = $name;
 }
 private function open() {
  return $this->name . "---打开电源,正在开机";
 }
 public function ok() {
  return $this->open() . "---OK";
 }
}
$pc = new MyPc("我的电脑");
echo $pc->ok();
?>

 


class MyPc {
private $name;
function __construct($name = "") {

Return $this->name = $name;

}

private function open() {

Return $this->name . "---Turn on the power, booting";

}
 代码如下 复制代码

class MyPc {
private $name;
public function __construct($name = "") {
return $this->name = $name;
 }

 public function __get($name) {
  return $this->name . "__get";
 }
 public function __set($n, $v) { //$n对应对象属性$name,$v对应倒数第三行__set。
  $this-> $n = $v;
 }
 private function open() {
  return $this->name . "---打开电源,正在开机";
 }
 public function ok() {
  return $this->open() . "---OK";
 }
}
$pc = new MyPc("我的电脑");
$pc->name = "__set";
echo $pc->name;
?>

public function ok() {

Return $this->open() . "---OK"; } } $pc = new MyPc("My Computer"); echo $pc->ok();

?>



Packaging related functions: __set(), __get() Function: operate private attributes or methods.

__set(): Get the encapsulated private properties or private methods in the current class, and re-execute or assign the value.

Format: __set($n,$v)

__get(): Get (indirect access to prevent naked output) the encapsulated attributes or methods in the current class and convert them into public attributes.

__get/__set instance:

The code is as follows Copy code

代码如下 复制代码

class MyPc {
...
}

class Home extends MyPc {
...
}

class MyPc {

private $name;
public function __construct($name = "") {

Return $this->name = $name;

}

public function __get($name) {
Return $this->name . "__get";

}
 代码如下 复制代码

class Root {
public function print1() {
return "Root_print";
}
}

class Son extends Root {
public function print2() {
return "Son_print";
}
}

$p = new Son();
echo $p->print1();
?>

public function __set($n, $v) { //$n corresponds to the object attribute $name, $v corresponds to the third to last row __set. $this-> $n = $v; } private function open() { Return $this->name . "---Turn on the power, booting"; } public function ok() { Return $this->open() . "---OK"; } } $pc = new MyPc("My Computer"); $pc->name = "__set"; echo $pc->name; ?>
Inheritance and Application of Classes 1. Inherit keyword: extends. 2. PHP inheritance rules. 3. Base class method overloading and base class method access. -------------------------------------------------- - 1. Inherit keyword: extends Inheritance of PHP classes can be understood as classes that share the inherited class (base class). Note: PHP is single inheritance. 2. Format
The code is as follows Copy code
class MyPc { ... } class Home extends MyPc { ... }
3. Base class method overloading and base class method access Format: base class name::original base class method name 4. Example Inheritance:
The code is as follows Copy code
class Root {<🎜> public function print1() {<🎜> Return "Root_print";<🎜> }<🎜> }<🎜> <🎜>class Son extends Root {<🎜> public function print2() {<🎜> Return "Son_print";<🎜> }<🎜> }<🎜> <🎜>$p = new Son();<🎜> echo $p->print1(); ?>

Reload:
If a method of the base class needs to be enhanced in the derived class, you can use the overload function

3. Rules for using abstract classes and methods

The code is as follows
 代码如下 复制代码

class Root {
 public function print1() {
  return "Root_print";
 }
}

class Son extends Root {
 public function print1() {
  return Root :: print1() . "
Son_print";
 }
}

$p = new Son();
echo $p->print1();
?>


 

Copy code

class Root { public function print1() {

Return "Root_print";

}

}

class Son extends Root {

public function print1() {

return Root :: print1() . "
Son_print";

}

}

$p = new Son();

echo $p->print1();

?>






Abstract methods and classes of classes

In fact, it can also be understood that this is a norm. Define an abstract class and methods at the beginning of the class, and then inherit the abstract class in the following classes. This can force the standard naming of the methods of the following derived classes (it is just the name of the abstract method defined in the abstract class, you can also add it yourself, but mainly cannot be modified).


1. Abstract keyword: abstract.
2. Definition of abstract methods and abstract classes.
3. Rules for using abstract classes and methods.

----------------------------------------

1. Abstract keyword: abstract

Abstraction means that it cannot be explained exactly, but it has a certain concept or name.


2. Definition of abstract methods and abstract classes
A class that has at least one method that is abstract is called an abstract class.

So if you define an abstract class, first define the abstract method.

Format:
abstract class class1 {

abstract function fun1();

...
}

Note: 1. There is at least one abstract method in the class. ;2. Abstract methods are not allowed to have {}. ;3. Abstract must be added in front of abstract methods.
 代码如下 复制代码

abstract class ChouXiang {
abstract function fun1();

}

class PaiShengLei extends ChouXiang {
public function fun1() { //重载
echo "test";
}
}

$p = new PaiShengLei();
$p->fun1();
?>

Abstract class features: 1. It cannot be instantiated and can only be inherited.

2. In the inherited derived class, all abstract methods must be overloaded before they can be instantiated.

Format (cannot be instantiated): abstract function fun1(); ... } Format (can be instantiated): class cl2 extends cl1 { function fun1() { //Overload abstract method }
... }
-------------------------------------------------- ----
Example:
The code is as follows Copy code
abstract class ChouXiang {<🎜> abstract function fun1();<🎜> <🎜>}<🎜> <🎜>class PaiShengLei extends ChouXiang {<🎜> public function fun1() { //Overload<🎜> echo "test";<🎜> }<🎜> }<🎜> <🎜>$p = new PaiShengLei();<🎜> $p->fun1(); ?> http://www.bkjia.com/PHPjc/632644.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/632644.htmlTechArticleObject-oriented programming (English: Object-oriented programming, abbreviation: OOP), refers to a programming paradigm , and it is also a method of program development. Object refers to a collection of classes...
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