【问题标题】:PHP namespace class not being found找不到 PHP 命名空间类
【发布时间】:2013-11-01 01:31:07
【问题描述】:

首先,这是流程,以便您了解问题。我有四个文件,其中三个是类,我使用两个命名空间。
login.php 是一个表单,当表单被提交时,它会回到自己并执行下面的代码。 login.php 调用 Zcrypt::Decrypt 和 Zcrypt::Encrypt 没有问题。登录::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 而在其他课程中却不能?我错过了一些让它工作的代码吗?

这是我的文件

登录.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";
}
}

登录.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]
    }
}

感谢您的帮助。

【问题讨论】:

  • 在第 13 行的 DB.class.php 中,如果我取消注释这一行,它会找到 Zcrypt,但是,当我运行它时,我在 ( 上出现语法错误。为什么?

标签: php class namespaces


【解决方案1】:

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

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

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

【讨论】:

  • 我让 db.class.php 看起来像这样
    ' namespace net[domain]\helpdesk;使用 \net[域]\Zcrypt;类 DB { public $dbHost = '[地址]';公共 $dbUser = '[un]';公共 $dbPass = '';公共 $dbName = '[名称]';公共$db; public function __construct(){} public function dbConnect() { #redefine $dbPass in this function $this->dbPass = Zcrypt::Dectrypt('[string]'); [代码] } } ' 感谢您的帮助。
【解决方案2】:

您必须使用\net\[domain]\Zcrypt:: 才能使其工作。或者更好的是分配一个别名,如use \net\[domain] as z,然后是z\Zcrypt::。换句话说,请参阅 PHP 手册 http://php.net/manual/en/language.namespaces.php 。查找file4。它有你需要的例子。

【讨论】:

  • 我在 Zcrypt 页面上有 \net[domain],因为 Zcrypt 是类,而不是名称空间。如果你在 Login.class.php 页面中记下,我可以调用 Zcrypt:: 并且它工作正常,这是为什么呢?
  • 我发现如果我使用 \Zcrypt:: 它会找到命名空间,但是,我在 ( 上出现错误,就像它找不到函数一样。
  • 它在 login.php 中是必需的,如果我在每个类中都添加了一个 require,我会收到一个错误,我正在定义重复的类。 require 只不过是一个 include ,所以基本上在 login.php 页面运行时,所有四页代码都合二为一,因此只需要一个 require。对吗?
  • 你使用的\net\[domain]\Zcrypt;应该是use \net\[domain];。
  • 我认为当你调用命名空间时,它后面跟着类名,因为 PHP 不支持 * [namespace][class] \net[domain]\Zcrypt;
猜你喜欢
  • 2017-05-01
  • 1970-01-01
  • 2012-01-26
  • 2016-08-15
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 1970-01-01
相关资源
最近更新 更多