PHP operation private variables (use of getxxx and setXXX)

From , 2 Years ago, written in PHP, viewed 167 times.
URL https://pastebin.vip/view/5e751896
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
  4. <title>操作私有变量</title>
  5. </head>
  6.  
  7. <body>
  8. <?php
  9. class Book{
  10.         private $name = 'computer';                                                     //声明私有变量$name
  11.         public function setName($name){                                         //设置私有变量方法
  12.                 $this -> name = $name;
  13.         }
  14.         public function getName(){                                                      //读取私有变量方法
  15.                 return $this -> name;
  16.         }
  17. }
  18. class LBook extends Book{                                                               //Book类的子类     
  19. }
  20. $lbook = new LBook();                                                                   //实例化对象
  21. echo '正确操作私有变量的方法:';                                                    //正确操作私有变量
  22. $lbook -> setName("PHP从入门到精通");
  23. echo $lbook -> getName();
  24. echo '<br>直接操作私有变量的结果:';                                                //错误操作私有变量
  25. echo Book::$name;
  26. ?>
  27. </body>
  28. </html>
  29.  

Reply to "PHP operation private variables (use of getxxx and setXXX)"

Here you can reply to the paste above

captcha

https://burned.cc - Burn After Reading Website