找不到PHP命名空间类

时间:2013-11-01 01:31:07

标签: php class namespaces

首先,这是流程,以便您可以理解问题。我有四个文件,其中三个是类,我使用两个名称空间 login.php是一个表单,当表单被提交时,它返回到自身并执行下面的代码。 login.php调用Zcrypt :: Decrypt和Zcrypt :: Encrypt没有问题。 Login :: DoLogin();也在login.php文件中调用。

在Login.class.php(DoLogin所在的文件)文件中,我创建了一个新的DB实例,并且可以在没有错误的情况下调用Zcrypt :: Decrypt。在Login.class.php中,我调用dbConnect();

在DB.class.php(dbConnect所在的文件)文件中,我无法调用Zcrypt :: Decrypt。它给我一个语法错误或它找不到Zcrypt。我尝试过Zcrypt :: Decrypt([string]),\ Zcrypt :: Decrypt([string]),甚至还有\ Zcrypt :: Decrypt([string])。

问题是,为什么我可以在某些课程中呼叫Zcrypt而不是其他课程?我错过了一些代码可以使用吗?

这是我的文件

的login.php:

require 'NS/helpdesk/Login.class.php';
require 'NS/helpdesk/Cryptv2.class.php';
require 'NS/helpdesk/DB.class.php';
use \net\[domain]\Zcrypt;
use \net\[domain]\helpdesk\Login;

#check to see if the form was submited and that the values are equal.
{
if (strlen($_POST['hvalue']) > 1 && $_SERVER['REMOTE_ADDR'] == Zcrypt::Decrypt($_POST['hvalue']) )
{
        Login::DoLogin();  ###### This is where I call my static Login Class
}
else {
    echo "bad form";
}
}

Login.class.php

namespace net\[domain]\helpdesk;

use \net\[domain]\helpdesk\DB;
use \net\[domain]\Zcrypt;
class Login
{

    public function DoLogin()
    {
        #call to the database class to open the db
        $DB = new DB();
        $DB->dbConnect();

        #This is to show I can call Zcrypt in this class (note, no \) and it works.     
        echo $dbPass = Zcrypt::Decrypt("[coded string]");   
    }
}

DB.class.php

namespace net\[domain]\helpdesk;

use \net\[domain]\Zcrypt;

class DB
{

    public $dbHost = '[address]';
    public $dbUser = '[un]';
    public $dbPass = '[pw]';  
  ######The two commented out lines below will not run.  I get a syntax error
    # public $dbPass = \Zcrypt::Decrypt("[strint]"); 
    # public $dbPass = Zcrypt::Decrypt("[string]")                   
    public $dbName = '[name]';
    public $db;

    public function __construct(){}

    public function dbConnect()
    {    
        [code]
    }
}

Cryptv2.class.php

namespace net\[domain];

use Exception;


class Zcrypt
{
    public static function Encrypt($i)
    {
                [code]  
    }
    public static function Decrypt($i)
    {
        [code]
    }
}

感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

这是语法错误。您不能在属性定义中使用表达式。

  

此声明可能包括初始化,但此初始化必须是常量值 - 也就是说,它必须能够在编译时进行评估,并且必须不依赖于运行时信息才能进行评估。

http://php.net/manual/en/language.oop5.properties.php

答案 1 :(得分:0)

您必须使用\net\[domain]\Zcrypt::才能使用此功能。或者更好地指定use \net\[domain] as z之类的别名,然后z\Zcrypt::。换句话说,请参阅PHP手册http://php.net/manual/en/language.namespaces.php。查找file4。它有你需要的例子。

相关问题