0

i made a custom class loader function in php something like..

load_class($className,$parameters,$instantiate);

its supposed to include the class and optionally instantiate the class specified

the problem is about the parameters. ive been trying to pass the parameters all day i tried

load_class('className',"'param1','param2'",TRUE);

and

load_class('className',array('param1','param2'),TRUE);

luckily nothing works xD is it possible to pass the params? i even tried..

$clas = new MyClass(array('param1','param2'));

here it is..

function load_class($class, $param=null, $instantiate=FALSE){
    $object = array();
    $object['is_required'] = require_once(CLASSES.$class.'.php');
    if($instantiate AND $object['is_required']){
        $object[$class] = new $class($param);
    }
    return $object;
}
kapitanluffy
  • 1,269
  • 7
  • 26
  • 54
  • 4
    You might want to show your load_class function. – kovshenin Oct 29 '10 at 10:17
  • possible duplicate of [Pass arguments from array in php to constructor](http://stackoverflow.com/questions/3395914/pass-arguments-from-array-in-php-to-constructor) – Gordon Oct 29 '10 at 10:25
  • Do you get any error messages? – Nev Stokes Oct 29 '10 at 10:46
  • yes..it says that the parameters ($param) for the constructor of the class is not passed.. – kapitanluffy Oct 29 '10 at 10:48
  • @Gordon i did try this too call_user_func_array(array($classname, '__construct'), $ArrayOfArgs); – kapitanluffy Oct 29 '10 at 10:52
  • 1
    @kapitan call_user_func does not work with constructors afaik. so you have to use the Reflection method as shown in the linked question. – Gordon Oct 29 '10 at 11:06
  • yup i did..thanks anyway. i finally made it work. is this reflection method already a part of PHP or is it something like pear that needs to be installed? – kapitanluffy Oct 29 '10 at 11:12
  • @kapitan it's part of PHP. On a sidenote, instead of requiring the classes, you might also want to consider using `spl_autoload_register` to include classes into your code. – Gordon Oct 29 '10 at 11:13

2 Answers2

3

if you are in PHP 5.x I really really recommend you to use autoload. Prior to PHP 5.3 you should create sort of "namespace" (I usually do this with _ (underscore))

autoload allows you to include classes on the fly and if your classes are well designed the overhead is minimun.

usually my autoload function looks like:

<?php
function __autoload($className) {
    $base = dirname(__FILE__);
    $path = explode('_', $className);

    $class = strtolower(implode('/',$path));

    $file = $base . "/" . $class;       

    if (file_exists($file)) {
        require $file;      
    }
    else {
        error_log('Class "' . $className . '" could not be autoloaded');
        throw new Exception('Class "' . $className . '" could not be autoloaded from: '.$file); 
    }
}

this way calling

$car = new App_Model_Car(array('color' => 'red', 'brand' => 'ford'));

the function will include the class

app/model/car.php

Gabriel Sosa
  • 7,897
  • 4
  • 38
  • 48
  • It is not wise to use __autoload() since it's been replaced by spl_autoload_register(). __autoload() Will be deprecated soon. http://www.php.net/manual/en/function.spl-autoload-register.php – Omar Oct 25 '13 at 19:45
1

Seems to me that you should be using __autoload() to just load classes as they are referenced and circumvent having to call this method manually. This is exactly what __autoload() is for.

Craige
  • 2,882
  • 2
  • 20
  • 28