- //File: a.php
- class a{
- static public function func_a(){
- class b{
- public function func_b(){
- echo 'executing class b function func_b';
- }
- }
- }
- }
- a::func_a();
- b::func_b();
复制代码 ---> Fatal error: Class declarations may not be nested in D:\xampplite\htdocs\lab\class\a.php on line 5- //File: a.php
- class a{
- static public function func_a(){
- eval("class b{
- public function func_b(){
- echo 'executing class b function func_b';
- }
- }");
- }
- }
- a::func_a();
- b::func_b();
复制代码 ---> executing class b function func_b- //File: a.php
- class a{
- static public function func_a(){
- include 'b.php';
- }
- }
- a::func_a();
- b::func_b();
- //File: b.php
- class b{
- public function func_b(){
- echo 'executing class b function func_b';
- }
- }
复制代码 ---> executing class b function func_b
------------------
function & method behaviors- class a{
- public static function func_a(){
- function func_c(){
- echo 'executing class a function func_c';
- }
- }
- }
- a::func_a(); //call to define nested function
- func_c();
- 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- class a{
- public function b(){
- $this = new c();
- $this->d();
- }
- }
- class c{
- public function d(){
- echo 'xxx';
- }
- }
- $a = new a();
- $a->b();
复制代码 ---> Fatal error: Cannot re-assign $this in D:\xampplite\htdocs\lab\class\index.php on line 5 |