开发者

include files in a method?

i want to have a class that includes all files for me.

eg.

Loader::loadZend // loads all zend libraries
Loader::loadSymfony // loads all symfony components

if i include a file in a method, does this become globally available?

it seems that it doesnt work.

maybe i have done something wrong, or is there a workar开发者_如何转开发ound for this?

thanks


From http://uk.php.net/manual/en/function.include.php (emphasis mine)

When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope.

Having a loader that loads all classes of Zend, Symfony or whatever at once is a bad idea. Both have their own autoloader, so all you need to do is make the libraries available on the include path and then spl_autoload_register the framework's own autoloader.


If no global scope variables are defined, you should be fine. Otherwise, the globals will be lost.

For example,

a.php:

<?php
$a = "global a";
class c {
  public $b = "member b"; 
}
?>

b.php

<?php
function inc() {
include 'a.php';
}

inc();

echo $a;

$class = new c;
echo $class->b;
?>

If you run b.php, you will find out $a is gone and class c or any function will still be available.


If you just include a class inside another class it is only available in the class you inserted the include.
So the solution is to create an autoloader that automaticly includes the classes needed on demmand. Here you have the info you need:

http://php.net/manual/es/language.oop5.autoload.php

It is basic programming for PHP5 OOP.

EDIT

Seeing your inquiry about a file. Neither. The file (to be processed via php functions) will only be available inside the function where it is called/used for, not globally in the whole application / website.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜