search
Homephp教程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

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

  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> ?>

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;
<span style="color: #008080;">1</span> <span style="color: #800080;">$runoob</span>->setTitle( "菜鸟教程" );

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

 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> ?>

 The execution result is:

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

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

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools