Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/271.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 类方法调用没有包含/要求的其他类方法?_Php_Zend Framework - Fatal编程技术网

Php 类方法调用没有包含/要求的其他类方法?

Php 类方法调用没有包含/要求的其他类方法?,php,zend-framework,Php,Zend Framework,我不知道如何在搜索引擎中表达这个问题 我有点搞不清楚类如何在没有include/require的情况下使用其他类的方法。我已经在PHP中看到过几次了。C++背景下,我必须把所有的东西都包括进去,所以有点混淆。 但好了,我们开始: 假设我有改进\u db\u connection.php: class improve_db_connection { protected $_connection; public function __construct($host, $user,

我不知道如何在搜索引擎中表达这个问题

我有点搞不清楚类如何在没有include/require的情况下使用其他类的方法。我已经在PHP中看到过几次了。C++背景下,我必须把所有的东西都包括进去,所以有点混淆。 但好了,我们开始:

假设我有
改进\u db\u connection.php

class improve_db_connection
{

    protected $_connection;

    public function __construct($host, $user, $pwd, $db)
    {
        $this->_connection = @new mysqli($host, $user, $pwd, $db);
        if (mysqli_connect_error()) {
            throw new RuntimeException('Cannot access database: ' .
                mysqli_connect_error());
        }
    }

    public function getResultSet($sql)
    {
        $results = new Pos_MysqlImprovedResult($sql, $this->_connection);
        return $results;
    }
}
new Pos_MysqlImprovedResult
Pos_MysqlImprovedResult
类的一个实例,而不是改进数据库连接。没有声明包含类的
include
函数
Pos\u MysqlImproveResult
improve\u db\u connection.php
文件只知道类在哪里

是因为
Pos\u MysqlImproveResult.php
improve\u db\u connection.php
位于同一目录下吗


Zend框架也是如此。但是这些文件都在子目录中。我在
application/modules/SF/models
中有一个类,它使用
application/modules/SF/models/resources
中的另一个类函数。是因为这些类的Zend命名约定,Zend只是解析这些文件并将其添加到php的
\uu autoload

Zend Framework使用Zend\u Loader在引用类时自动加载类,只要它能在逻辑位置找到它们

我假设您正在运行一个基于Zend应用程序的脚本,它会自动为您配置自动加载器,并完成所有艰苦的工作。由于您的文件位于其逻辑文件夹中,并且类具有ZF约定名称,因此ZF将在您引用它们时查找并加载它们

更多信息请参见ZF手册:


Zend Framework使用Zend_Loader在引用类时自动加载类,只要它能在逻辑位置找到它们

我假设您正在运行一个基于Zend应用程序的脚本,它会自动为您配置自动加载器,并完成所有艰苦的工作。由于您的文件位于其逻辑文件夹中,并且类具有ZF约定名称,因此ZF将在您引用它们时查找并加载它们

更多信息请参见ZF手册:


为了补充Valorin的解释,PHP中有一个神奇的方法,在找不到类定义时尝试查找类定义。有关详细说明,请访问此处:

其要点是:

function __autoload ($class_name) {
    // write your logic to find the class name

    // iterate over the available paths
    foreach(explode(PATH_SEPARATOR, get_include_path()) as $path) {
        // create a local variable representing the file path to the would be include file
        $file = implode(DIRECTORY_SEPARATOR, array(
            $path, "{$class_name}.class.inc",
        ));

        // If the file doesn't exist, continue, avoiding further processing
        if (!file_exists($file)) { continue; }

        // require the file, and return to avoid further processing
        require_once $file;
        return;
    }
}

// provided there's a foo.class.inc file in one of your paths ...
$Foo = new Foo;

为了补充Valorin的解释,PHP中有一个神奇的方法,在找不到类定义时尝试查找类定义。有关详细说明,请访问此处:

其要点是:

function __autoload ($class_name) {
    // write your logic to find the class name

    // iterate over the available paths
    foreach(explode(PATH_SEPARATOR, get_include_path()) as $path) {
        // create a local variable representing the file path to the would be include file
        $file = implode(DIRECTORY_SEPARATOR, array(
            $path, "{$class_name}.class.inc",
        ));

        // If the file doesn't exist, continue, avoiding further processing
        if (!file_exists($file)) { continue; }

        // require the file, and return to avoid further processing
        require_once $file;
        return;
    }
}

// provided there's a foo.class.inc file in one of your paths ...
$Foo = new Foo;

如果它不是Zend应用程序,那么它仍然可以使用自动加载器,该自动加载器将在其中一个脚本文件中的某个位置定义,并且可以读取以确定在包含任何必要文件之前检查的路径。如果它不是Zend应用程序,那么它仍然可以使用自动加载器,该自动加载器将在其中一个脚本文件中的某个位置定义,可以读取,以确定在包含任何必要文件之前检查的路径。