3

I've searched a lot how to load/include a simple class in symfony 1.4 and i found nothing. I have this situation.

Under:

./apps/APP_NAME/
    modules/MOD_NAME/lib

I have a class:

class MyClass {
   function get_data(){
      retrun "data";
   }
}
  • Which is the best way to load in controller on symfony 1.4 the class?
  • Over on config exist some other way like function use_helper()?

Update: I want to load on demand, not to autoload with the convention of symfony 1.4 naming the file MyClass.class.php

onalbi
  • 2,609
  • 1
  • 24
  • 36
  • 1
    Is there some reason you can't `require 'MyClass.php';` the file and `use MyClass;` it? Not using the autoloader is a weird decision though. – HPierce Oct 09 '15 at 13:44
  • The problem of require || require_once is that are not flexible on module env. For example when i call use_helper() as second argument i can specify the module. – onalbi Oct 09 '15 at 15:35

2 Answers2

1

Is not the best but a first version that work under module ENV is:

    function load_module_lib($module, $name_file) {
       require_once (sfContext::getInstance()->getModuleDirectory() . '/../' . $module . '/lib/' . $name_file . '.class.php');
    }
onalbi
  • 2,609
  • 1
  • 24
  • 36
0

This file allows you to define extra classes that can be autoloaded when Symfony loads. and Symfony allows you to place php code inside a .yml file

##apps/appName/config/autoload.yml
autoload:
  makeupaname:
    name: Make up a name
    path: <?php echo $mydynamicvar; ?>/someFolder/
    recursive: on
   
  makeupasecondname:
    name: Make up a second name
    path: <?php echo $mydynamicvar; ?>someOtherFolder/
    recursive: on

as well as go thorough this extra links autoloading namespaces in symfony 1.4 and How to create/use custom classes and helper in symfony 1.4?

Edit 01

If you want to place externel library you have to place that in lib/vendor folder. You cant create that inside app folder.

As well as if you just add new library to that and site will not load your library. So that only you have to use the autoload file to do that

This article help you out. Loading external libraries for symfony 2

Community
  • 1
  • 1
Abdulla Nilam
  • 36,589
  • 17
  • 64
  • 85
  • 1
    This seems like the proper solution in a normal use case, but the question specifically calls out for _not_ using the autoloader and instead allowing the class to load to on demand - which I'm not sure this solution does. – HPierce Oct 15 '15 at 20:50