[PHP] php this的用法 →→→→→进入此内容的聊天室

来自 , 2021-01-29, 写在 PHP, 查看 108 次.
URL http://www.code666.cn/view/7e0a0209
  1. < ?php  
  2. class UserName  
  3. {  
  4. //定义属性  
  5. private $name;  
  6. //定义构造函数  
  7. function __construct( $name )  
  8. {  
  9. $this->name = $name;
  10. //这里已经使用了this指针  
  11. }  
  12. //析构函数  
  13. function __destruct(){}  
  14. //打印用户名成员函数  
  15. function printName()  
  16. {  
  17. print( $this->name );
  18. //又使用了PHP关键字this指针  
  19. }  
  20. }  
  21. //实例化对象  
  22. $nameObject = new UserName
  23. ( "heiyeluren" );  
  24. //执行打印  
  25. $nameObject->printName();
  26.  //输出: heiyeluren  
  27. //第二次实例化对象  
  28. $nameObject2 = new UserName( "PHP5" );  
  29. //执行打印  
  30. $nameObject2->printName(); //输出:PHP5  
  31. ?>

回复 "php this的用法"

这儿你可以回复上面这条便签

captcha