开发者

Laucher for external PHP objects

开发者 https://www.devze.com 2023-03-15 10:15 出处:网络
I would like to do something like: library.php: require_once \"laucher.php\"; class Test{ publi开发者_开发问答c function __construct(){

I would like to do something like:

library.php:

require_once "laucher.php";

    class Test{
        publi开发者_开发问答c function __construct(){
            print "test";
        }
    }

    class Foo extends Bar{
        public function __construct(){
            $t = new Test();
        }
    }

    class Bar{
        public function __construct(){

        }
    }

And in laucher.php, I would like to create a Foo object as $t = new Foo();

How Can I create Foo Objects in laucher.php? I would like to create an "auto-laucher" of Foo();


You have to include the file Foo is located in. So...

include("foo_file.php");

Then you can instantiate Foo.

$my_object = new Foo();


You cannot create Foo objects before Foo has been defined. Therefore you can't create Foo objects inside "laucher.php" if you include it before the class declarations.

However, if laucher.php is included after the class declarations you should be able to create Foo objects inside. So I think this would work:

class Test{
    public function __construct(){
        print "test";
    }
}

class Foo extends Bar{
    public function __construct(){
        $t = new Test();
    }
}

class Bar{
    public function __construct(){

    }
}

require_once "laucher.php";
0

精彩评论

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