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.