开发者

php require class call from inside method

开发者 https://www.devze.com 2022-12-28 17:45 出处:网络
from my 开发者_JS百科understanding, require pastes code into the calling php file. what if you were requiring from inside a method...it would paste the entire code/class inside the method, blocking t

from my 开发者_JS百科understanding, require pastes code into the calling php file.

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?


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.

File: Foo.php:

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

File: 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.


Calling require, require_once, include or include_once will not stop 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 scope as the function (with defined functions and classes being global), not inserted into it, replacing what else is there.

The other answers recommend using one of alternatives to require as 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.

On a further note, these file inclusion methods do not paste code into your script. Pasting is 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:

  • require
  • require_once
  • include
  • include_once

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


try require_once? Just incase your including it elsewhere.


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

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:

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();


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)


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号