php php需要从方法内部调用类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2755500/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 07:34:44  来源:igfitidea点击:

php require class call from inside method

phpoopclassrequire

提问by Jeremy Gwa

from my understanding, require pastes code into the calling php file.

根据我的理解,需要将代码粘贴到调用 php 文件中。

what if you were requiring from inside a method...it would paste the entire code/class inside the method, blocking the next statement in the method.

如果您从方法内部要求怎么办……它会将整个代码/类粘贴到该方法中,从而阻止该方法中的下一条语句。

eg.

例如。

  function test() {

    require 'pathtosomeclasscode';
    somestatement; // any code after the require is blocked.

 }

how do i get around this, to be able to require code where-ever, without it being pasted in that exact spot?

我如何解决这个问题,以便能够在任何地方都需要代码,而不会将其粘贴到那个确切的位置?

回答by timdev

Somewhat surprisingly, this appears to work just fine. The included code will execute inside the scope of Baz::bork() -- but the the code simply defines a class, and classes are global. So you end up with a defined class.

有点令人惊讶的是,这似乎工作得很好。包含的代码将在 Baz::bork() 的范围内执行——但代码只是定义了一个类,而类是全局的。所以你最终得到了一个定义好的类。

File: Foo.php:

文件:Foo.php:

<?PHP
class Foo{
      function bar(){
               echo "Hello from Foo::bar()!\n";
      }
}

File: Baz.php:

文件:Baz.php:

<?PHP
class Baz{

      function bork(){
               require_once "Foo.php";
               $f = new Foo();
           $f->bar();
      }
}

echo "testing internal definition:\n";
$b = new Baz();
$b->bork();

echo "\ntesting in global scope:\n";

$f = new Foo();
$f->bar();
echo "\nall done\n";

Output:

输出:

$ php Baz.php
testing internal definition:
Hello from Foo::bar()!

testing in global scope:
Hello from Foo::bar()!

all done

Now, I can't think of many places where you'd want to do things this way. People typically require_once() any possible dependencies at the top of their class file, outside of the class definition.

现在,我想不出很多地方你会想以这种方式做事。人们通常需要_once() 在他们的类文件的顶部,在类定义之外的任何可能的依赖项。

回答by icio

Calling require, require_once, includeor include_oncewill notstop the execution of code within your function unless there is a problem with the included script. The code within the included file is executed in the same scopeas the function (with defined functions and classes being global), not inserted into it, replacing what else is there.

调用require, require_once, includeorinclude_once不会停止函数内代码的执行,除非包含的脚本有问题。包含文件中的代码在与函数相同的范围内执行(定义的函数和类是全局的),而不是插入到其中,替换那里的其他内容。

The other answers recommend using one of alternatives to requireas a solution -- these solutions may work for you if the error in your scripts is to do with a conflict in naming (i.e. an attempt at redefining a class or function), otherwise you may wish to give us more details of what has occurred (or not occurred) to make you think that there is a problem so that we can help you solve the real cause.

其他答案建议使用替代require方案之一作为解决方案 - 如果脚本中的错误与命名冲突有关(即尝试重新定义类或函数),这些解决方案可能对您有用,否则您可能希望向我们提供已发生(或未发生)的更多详细信息,使您认为存在问题,以便我们帮助您解决真正的原因。

On a further note, these file inclusion methods do not pastecode into your script. Pastingis a tool that people use to maintain text. I suggest that you read the PHP manual to try and further your understanding of how these functions work:

进一步说明,这些文件包含方法不会将代码粘贴到您的脚本中。粘贴是人们用来维护文本的工具。我建议您阅读 PHP 手册以尝试进一步了解这些函数的工作原理:

Post again if you've got further questions on their working after looking into them.

如果您在调查后对他们的工作有进一步的问题,请再次发布。

回答by Titan

try require_once? Just incase your including it elsewhere.

试试require_once?以防万一您将其包含在其他地方。

回答by buley

If you use include_once('script.php') you won't run into errors about classes being redeclared.

如果你使用 include_once('script.php') 你不会遇到关于类被重新声明的错误。

I haven't tried the following myself, but I'm thinking this setup might also work if you don't want to include_once() redundantly:

我自己还没有尝试过以下操作,但我认为如果您不想多余地 include_once() ,此设置也可能有效:

class Whatever {

    protected $classObject;
    public function __construct( /*...*/ ) {
        include('anotherClass.php');
        $this->classObject = new anotherClass();
    }

    public function someFunction( /*...*/ ) {
        $this->classObject->classObjectFunction();
    }

    public function anotherFunction( /*...*/ ) {
        $this->classObject->classObjectFunction();
    }
}

You could then call the following code which would each call a function in the same shared class object:

然后,您可以调用以下代码,每个代码都会调用同一个共享类对象中的一个函数:

$Whatever = new Whatever();
$Whatever->someFunction();
$Whatever->anotherFunction();

回答by waiwai933

I think what you're saying is that you don't want the code to execute right away. If that is correcty, you have several options:

我认为您的意思是您不希望代码立即执行。如果这是正确的,您有几种选择:

  1. You can move the require.

  2. You can also read the file using fopen(), then parse it. (You can't call eval because it won't understand the HTML, but you can do it with a parser)

  1. 您可以移动需要。

  2. 您还可以使用 fopen() 读取文件,然后解析它。(你不能调用 eval 因为它不会理解 HTML,但你可以用解析器来做)

回答by waiwai933

As I see, you're using external files for loading classes, am I correct? Why don't you use Autoloading Classes. I know it won't work when you want to load different class bodies in different circumstances, but I really don't know if it's a good practice.

如我所见,您正在使用外部文件来加载类,对吗?为什么不使用Autoloading Classes。我知道当你想在不同的情况下加载不同的类体时它不会工作,但我真的不知道这是否是一个好的做法。