Home >Backend Development >PHP Tutorial >Internal reference of private method in PHP class, php class private reference_PHP tutorial

Internal reference of private method in PHP class, php class private reference_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:02:381323browse

Internal references to private methods in PHP classes, private references to PHP classes

I used to be used to art and front-end development, but now I have advanced to the back-end and started to learn PHP. Sometimes, I encountered a problem with private method calling.

The code is as follows:

<span> 1</span> <?<span>php
</span><span> 2</span> <span>class</span><span> Person { 
</span><span> 3</span>     <span>public</span> <span>function</span> say(<span>$name</span>, <span>$age</span><span>) {  
</span><span> 4</span>         <span>$con</span> = <span>$this</span>->get(<span>$name</span>, <span>$age</span>); <span>//</span><span>这里的$this->是关键</span>
<span> 5</span>         <span>echo</span> "my name is :".<span>$name</span>."<br />"<span>;  
</span><span> 6</span>         <span>echo</span> "my age is :".<span>$age</span>."<br/>"<span>;
</span><span> 7</span>         <span>echo</span> "get :".<span>$con</span>."<br/>"<span>;        
</span><span> 8</span> <span>    }
</span><span> 9</span>     <span>private</span> <span>function</span> get(<span>$a</span>, <span>$b</span><span>) {
</span><span>10</span>         <span>$a</span> = <span>$a</span>.<span>$b</span><span>;
</span><span>11</span>         <span>return</span> <span>$a</span><span>;
</span><span>12</span> <span>    }
</span><span>13</span> 
<span>14</span> <span>}  
</span><span>15</span> 
<span>16</span> <span>$person</span>=<span>new</span><span> Person();  
</span><span>17</span> <span>$person</span>->say("ren", 25<span>);
</span><span>18</span> 
<span>19</span> ?>        

This code is very simple. I want to implement the call of the internal private method of the class. As shown in line 4. I was used to writing JS in the past. When calling the get() method, if I didn't add "$this" in front, the program would have problems. After adding it, you can call it.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/970604.htmlTechArticleInternal references to private methods in PHP classes, PHP class private references used to be used in art and front-end development, now advanced Backend, I started learning PHP. When I was learning classes, I came across a private party...
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