Table of Contents
php from getting started to giving up series-03.php functions and object-oriented
1. Function
2. Object-oriented
 1. Basic class syntax:
 2. Create objects to use
 3. Constructor
 4. Destructor
 5. Inheritance
3. Conclusion
Home php教程 php手册 PHP from getting started to giving up series-03.php functions and object-oriented

PHP from getting started to giving up series-03.php functions and object-oriented

Aug 10, 2016 am 08:49 AM

php from getting started to giving up series-03.php functions and object-oriented

1. Function

 The real power of PHP comes from its functions. There are 1000 built-in functions. You can refer to the PHP reference manual.

 Custom function:

<span style="color: #008080;">1</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> functionName()
</span><span style="color: #008080;">2</span> <span style="color: #000000;">{
</span><span style="color: #008080;">3</span> <span style="color: #000000;">要执行的代码;
</span><span style="color: #008080;">4</span> }
Copy after login

  Guidelines for function naming:

  • The name of the function should indicate its function
  • Function name starts with a letter or underscore (cannot start with a number)

2. Object-oriented

 1. Basic class syntax:

<span style="color: #008080;"> 1</span> <?<span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> Site {
</span><span style="color: #008080;"> 3</span>   <span style="color: #008000;">/*</span><span style="color: #008000;"> 成员变量 </span><span style="color: #008000;">*/</span>
<span style="color: #008080;"> 4</span>   <span style="color: #0000ff;">var</span> <span style="color: #800080;">$url</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 5</span>   <span style="color: #0000ff;">var</span> <span style="color: #800080;">$title</span><span style="color: #000000;">;
</span><span style="color: #008080;"> 6</span>   
<span style="color: #008080;"> 7</span>   <span style="color: #008000;">/*</span><span style="color: #008000;"> 成员函数 </span><span style="color: #008000;">*/</span>
<span style="color: #008080;"> 8</span>   <span style="color: #0000ff;">function</span> setUrl(<span style="color: #800080;">$par</span><span style="color: #000000;">){
</span><span style="color: #008080;"> 9</span>      <span style="color: #800080;">$this</span>->url = <span style="color: #800080;">$par</span><span style="color: #000000;">;
</span><span style="color: #008080;">10</span> <span style="color: #000000;">  }
</span><span style="color: #008080;">11</span>   
<span style="color: #008080;">12</span>   <span style="color: #0000ff;">function</span><span style="color: #000000;"> getUrl(){
</span><span style="color: #008080;">13</span>      <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>->url . <span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;
</span><span style="color: #008080;">14</span> <span style="color: #000000;">  }
</span><span style="color: #008080;">15</span>   
<span style="color: #008080;">16</span>   <span style="color: #0000ff;">function</span> setTitle(<span style="color: #800080;">$par</span><span style="color: #000000;">){
</span><span style="color: #008080;">17</span>      <span style="color: #800080;">$this</span>->title = <span style="color: #800080;">$par</span><span style="color: #000000;">;
</span><span style="color: #008080;">18</span> <span style="color: #000000;">  }
</span><span style="color: #008080;">19</span>   
<span style="color: #008080;">20</span>   <span style="color: #0000ff;">function</span><span style="color: #000000;"> getTitle(){
</span><span style="color: #008080;">21</span>      <span style="color: #0000ff;">echo</span> <span style="color: #800080;">$this</span>->title . <span style="color: #ff00ff;">PHP_EOL</span><span style="color: #000000;">;
</span><span style="color: #008080;">22</span> <span style="color: #000000;">  }
</span><span style="color: #008080;">23</span> <span style="color: #000000;">}
</span><span style="color: #008080;">24</span> ?>
Copy after login

The analysis is as follows:

  • Class uses the class keyword followed by the class name definition.

  • Variables and methods can be defined within a pair of curly brackets ({}) after the class name.

  • Class variables are declared using var, and variables can also be initialized.

  • Function definitions are similar to PHP function definitions, but functions can only be accessedthrough the class and the objects it instantiates.

  • $this represents its own object, access object members using -> access, no longer.
  • PHP_EOL is the newline character

 2. Create objects to use

 After the class is created, we can use the new operator to instantiate the object of the class. To access the object members, use -> access, no longer., use new Site to call the no-argument constructor without parentheses :

<span style="color: #008080;">1</span> <span style="color: #800080;">$runoob</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Site;
</span><span style="color: #008080;">2</span> <span style="color: #800080;">$taobao</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> Site;
</span><span style="color: #008080;">3</span> <span style="color: #800080;">$google</span> = <span style="color: #0000ff;">new</span> Site;
Copy after login
<span style="color: #008080;">1</span> <span style="color: #800080;">$runoob</span>->setTitle( "菜鸟教程" );
Copy after login

 3. Constructor

Use _construct to name the constructor

<span style="color: #008080;">1</span> <span style="color: #0000ff;">function</span> __construct( <span style="color: #800080;">$par1</span>, <span style="color: #800080;">$par2</span><span style="color: #000000;"> ) {
</span><span style="color: #008080;">2</span>    <span style="color: #800080;">$this</span>->url = <span style="color: #800080;">$par1</span><span style="color: #000000;">;
</span><span style="color: #008080;">3</span>    <span style="color: #800080;">$this</span>->title = <span style="color: #800080;">$par2</span><span style="color: #000000;">;
</span><span style="color: #008080;">4</span> }
Copy after login

 4. Destructor

 Contrary to the constructor, when the object ends its life cycle (for example, the function in which the object is located has been called), the system automatically executes the destructor

<span style="color: #008080;"> 1</span> <?<span style="color: #000000;">php
</span><span style="color: #008080;"> 2</span> <span style="color: #0000ff;">class</span><span style="color: #000000;"> MyDestructableClass {
</span><span style="color: #008080;"> 3</span>    <span style="color: #0000ff;">function</span><span style="color: #000000;"> __construct() {
</span><span style="color: #008080;"> 4</span>        <span style="color: #0000ff;">print</span> "构造函数\n"<span style="color: #000000;">;
</span><span style="color: #008080;"> 5</span>        <span style="color: #800080;">$this</span>->name = "MyDestructableClass"<span style="color: #000000;">;
</span><span style="color: #008080;"> 6</span> <span style="color: #000000;">   }
</span><span style="color: #008080;"> 7</span> 
<span style="color: #008080;"> 8</span>    <span style="color: #0000ff;">function</span><span style="color: #000000;"> __destruct() {
</span><span style="color: #008080;"> 9</span>        <span style="color: #0000ff;">print</span> "销毁 " . <span style="color: #800080;">$this</span>->name . "\n"<span style="color: #000000;">;
</span><span style="color: #008080;">10</span> <span style="color: #000000;">   }
</span><span style="color: #008080;">11</span> <span style="color: #000000;">}
</span><span style="color: #008080;">12</span> 
<span style="color: #008080;">13</span> <span style="color: #800080;">$obj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> MyDestructableClass();
</span><span style="color: #008080;">14</span> ?>
Copy after login

 The execution result is:

<span style="color: #000000;">构造函数
销毁 MyDestructableClass</span>
Copy after login

 5. Inheritance

  Single root inheritance, inheritance uses the keyword extends, and interface implementation uses implements

3. Conclusion

 OK, you have finished learning PHP functions and objects. Note that this tutorial is a quick learning tutorial and only focuses on some grammatical key points and special and different points.

Statement of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)