Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/240.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_Class_Methods - Fatal编程技术网

PHP-为所有要使用的方法定义类中包含的文件

PHP-为所有要使用的方法定义类中包含的文件,php,class,methods,Php,Class,Methods,是否有一种方法可以在类的开头包含一个文件供所有方法使用。下面的例子是我试图实现的一个简化版本。目前,我必须在每个方法中包含该文件 示例逻辑不起作用 class Myclass { protected require_once 'folerd1/folder2/pear/HTTP/Request2.php'; // this does not work public function aMethod() {

是否有一种方法可以在类的开头包含一个文件供所有方法使用。下面的例子是我试图实现的一个简化版本。目前,我必须在每个方法中包含该文件

示例逻辑不起作用

    class Myclass
    {
        protected require_once 'folerd1/folder2/pear/HTTP/Request2.php'; // this does not work

        public function aMethod()
        {
            $request = new HTTP_Request2('http://example1.com/', HTTP_Request2::METHOD_GET);
            try {
                $response = $request->send();
                if (200 == $response->getStatus()) {
                    echo $response->getBody();
                } else {
                    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
                        $response->getReasonPhrase();
                }
            } catch (HTTP_Request2_Exception $e) {
                echo 'Error: ' . $e->getMessage();
            }
        }

        public function aMethod1()
        {
            $request = new HTTP_Request2('http://example2.com/', HTTP_Request2::METHOD_GET);
            try {
                $response = $request->send();
                if (200 == $response->getStatus()) {
                    echo $response->getBody();
                } else {
                    echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
                        $response->getReasonPhrase();
                }
            } catch (HTTP_Request2_Exception $e) {
                echo 'Error: ' . $e->getMessage();
            }
        }

        // more methods
    }

最好的方法是查看自动加载标准

你试过这个吗?我不知道你的Request2.php文件内容是什么。所以我的代码只是一个PoC

require 'folerd1/folder2/pear/HTTP/Request2.php'; 

class Myclass
{
    public function aMethod()
    {
        $request = new Request2('http://example1.com/', Request2::METHOD_GET);
        try {
            $response = $request->send();
            if (200 == $response->getStatus()) {
                echo $response->getBody();
            } else {
                echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
                    $response->getReasonPhrase();
            }
        } catch (Request2_Exception $e) {
            echo 'Error: ' . $e->getMessage();
        }
    }

    public function aMethod1()
    {
        $request = new Request2('http://example2.com/', Request2::METHOD_GET);
        try {
            $response = $request->send();
            if (200 == $response->getStatus()) {
                echo $response->getBody();
            } else {
                echo 'Unexpected HTTP status: ' . $response->getStatus() . ' ' .
                    $response->getReasonPhrase();
            }
        } catch (Request2_Exception $e) {
            echo 'Error: ' . $e->getMessage();
        }
    }
}
2解决方案

一旦从课堂上带到外面,我就要求你

require_once 'folerd1/folder2/pear/HTTP/Request2.php';     
class Myclass
{
2包括在构造中

public function __construct() {
    require_once 'folerd1/folder2/pear/HTTP/Request2.php';
}

但最好是第一个选项,正如我所评论的那样,只需要PHP类构造函数中的文件:

class Myclass{
    function Myclass(){
        require_once('folerd1/folder2/pear/HTTP/Request2.php');
    }
我还没有检查过这个,但可能您刚才所做的不仅包括所有类都可以看到的内容,还包括整个脚本。如果这对你来说是个问题,请检查一下。否则,其他两个解决方案可能最终也会这样做


干杯

您是否尝试在构造函数中要求它?构造函数将为变量赋值。我不确定如何在构造方法中执行类似的操作。我以前从未见过它,我根本不是大师,但你不能调用require_oncefored1/folder2/pear/HTTP/Request2.php;关于构造函数?它会加载任何变量,我想你可以在整个类中使用它们。有没有理由将require设置为protected?我刚刚试过你的建议。事实上,谢谢确实管用。你能把这个作为答案吗?我会把它标记为正确的。这样它也会帮助其他人。是的,对不起-没有保护!第二个解决方案完成了任务。我是根据@Timinus的建议做这件事的