Home > php教程 > php手册 > body text

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

WBOY
Release: 2016-08-10 08:49:35
Original
1343 people have browsed it

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.

source:php.cn
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!