2011-09-09 24 views
0

是否可以將$ this對象分配給擴展$ this基類的類的新實例? (當然在PHP)

class base { 

    function a() { 

     // do some stuff here 
     $extended = new extending(); 
     $this = $extended; 
     $this->extended_functionality(); 
    } 
} 

class extending { 

    function extended_functionality() { 

     // do some more! stuff here 
    } 
} 
+0

到底爲什麼要這麼做? –

+1

這是一種反模式。不管它是否可能,我都會建議你避免它。 –

+0

嗯做了更多的測試,看起來像你不能這樣做......也許有人可以指導我更多的語義方法? – AndrewMcLagan

回答

3

在短...號

$this是隻讀關鍵字表示當前實例化的對象。

只要首先創建您的意思的對象。

0

不,你應該:

class base { 

private extended; 

    function a() { 

     // do some stuff here 
     $this->extended = new extending(); 
     $this->extended->extended_functionality(); 
    } 
} 

class extending { 

    function __construct() { 
    } 

    function extended_functionality() { 

     // do some more! stuff here 
    } 
} 
相關問題