找回密码
 新猫注册
查看: 1392|回复: 0

Classes & objects behaviors testing in php5

[复制链接]
kernel 发表于 2009-5-4 12:56:24 | 显示全部楼层 |阅读模式
  1. //File: a.php
  2. class a{

  3.         static public function func_a(){
  4.                 class b{
  5.                         public function func_b(){
  6.                                 echo 'executing class b function func_b';
  7.                         }
  8.                 }
  9.         }

  10. }

  11. a::func_a();
  12. b::func_b();
复制代码
---> Fatal error: Class declarations may not be nested in D:\xampplite\htdocs\lab\class\a.php on line 5
  1. //File: a.php
  2. class a{

  3.         static public function func_a(){
  4.                 eval("class b{
  5.                         public function func_b(){
  6.                                 echo 'executing class b function func_b';
  7.                         }
  8.                 }");
  9.         }

  10. }

  11. a::func_a();
  12. b::func_b();
复制代码
---> executing class b function func_b
  1. //File: a.php
  2. class a{

  3.         static public function func_a(){
  4.                 include 'b.php';
  5.         }

  6. }

  7. a::func_a();
  8. b::func_b();

  9. //File: b.php
  10. class b{
  11.         public function func_b(){
  12.                 echo 'executing class b function func_b';
  13.         }
  14. }
复制代码
---> executing class b function func_b

------------------

function & method behaviors
  1. class a{

  2.         public static function func_a(){
  3.                 function func_c(){
  4.                         echo 'executing class a function func_c';
  5.                 }
  6.         }

  7. }

  8. a::func_a();    //call to define nested function


  9. func_c();
  10. a::func_c();
复制代码
func_c(); -> executing class a function func_c
a::func_c(); -> Fatal error: Call to undefined method a::func_c() in D:\xampplite\htdocs\lab\class\a.php on line 14

conclusion: define a function in a method will be a global function but another method, same as being defined by eval.

-------------------

about $this, seems it's readonly
  1. class a{

  2.         public function b(){
  3.                 $this = new c();
  4.                 $this->d();
  5.         }

  6. }

  7. class c{

  8.         public function d(){
  9.                 echo 'xxx';
  10.         }

  11. }

  12. $a = new a();

  13. $a->b();
复制代码
---> Fatal error: Cannot re-assign $this in D:\xampplite\htdocs\lab\class\index.php on line 5
您需要登录后才可以回帖 登录 | 新猫注册

本版积分规则

手机版|小黑屋|[漫猫]动漫论坛

GMT+8, 2024-7-16 18:02

Powered by Discuz! X3.5

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表