tags:

views:

26

answers:

3

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 workaround for this?

thanks

+1  A: 

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.

Gordon
i think u misunderstood (or perhaps i misunderstood the reading you copied) i want to call a function that includes a file. will this file included from the method be available for all code in the global scope?
never_had_a_name
@fayer like it says above: if you `include` a file, variables in the file will be available inside the calling function only, while functions and classes in the file will be globally available.
Gordon
ok then....then i guess i just use spl_autoload_register. thanks.
never_had_a_name
@gordon. do you which is the Zend function for the spl_autoload_register?
never_had_a_name
@fayer not sure. Try `spl_autoload_register(array('Zend_Loader', 'autoload'));`
Gordon
@gordon. it says: Zend_Loader::Zend_Loader::autoload is deprecated as of 1.8.0 and will be removed with 2.0.0; use Zend_Loader_Autoloader. which function are they referring me to use?
never_had_a_name
@fayer hmmm.. maybe `spl_autoload_register(array('Zend_Loader_Autoloader', 'autoload'));`
Gordon
+1  A: 

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.

Dez
yeah im using spl_autoload but im interested in to know if this function will work 'inside' a method. and if i don't use autoload, just a regular include, will the included file from the method be available for all code in the global scope?
never_had_a_name
A regular include it will only affect to the class where it is included. Gordon nailed it.
Dez
+2  A: 

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.

ZZ Coder