2014-07-08 42 views
0

我有一个以下的php类。PHP类在旧版本中无法正常工作

<?php 

class t extends c{ 
    function __construct() {  
    parent::__construct(); 
    } 
} 
class c extends d{ 
    function __construct() {  
    parent::__construct(); 
    } 
} 
class d { 
    function __construct() {   
    echo "worked"; 
    } 
} 

new t(); 
?> 

上述类在我的本地机器工作得很好,PHP版本为(PHP版5.5.9-1ubuntu4.2)

,但它不是在PHP版本是云服务器的工作(PHP版本5.4.26和Linux主机) 我有另一台服务器的PHP版本是(PHP版本5.3.28 amazone云服务器) 这里上面的代码也无法正常工作。

任何想法为什么它不能在上述两个PHP版本(5.4.26和5.3.28)中工作?

+0

你所说的“并不意味着不工作“?你有错误吗? –

回答

1

你在这里看到this example that you get this error

Fatal error: Class 'c' not found in /tmp/execpad-0fdb5d0d9043/source-0fdb5d0d9043 on line 3

如果您打开类声明的顺序各地使其在逻辑(程序我想)定义,you get what you expect(PHP 5.4.6):

class d { /* etc */ } 

class c extends d { /* etc */ } 

class t extends c { /* etc */ } 

new t(); // worked 

PHP manual on object inheritance

NOTE: Unless autoloading is used, then classes must be defined before they are used. If a class extends another, then the parent class must be declared before the child class structure. This rule applies to classes that inherit other classes and interfaces.

+1

谢谢先生。它非常重要而且至关重要 – Samiul

相关问题