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;
}
8
  • 4
    You might want to show your load_class function. Commented Oct 29, 2010 at 10:17
  • possible duplicate of Pass arguments from array in php to constructor Commented Oct 29, 2010 at 10:25
  • Do you get any error messages? Commented Oct 29, 2010 at 10:46
  • yes..it says that the parameters ($param) for the constructor of the class is not passed.. Commented Oct 29, 2010 at 10:48
  • 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. Commented Oct 29, 2010 at 11:06

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

1 Comment

It is not wise to use __autoload() since it's been replaced by spl_autoload_register(). __autoload() Will be deprecated soon. php.net/manual/en/function.spl-autoload-register.php
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.