Can PHP do magical creation?

class RandomName extends CommonAppBase {}Is there a way for a class to automatically instantiate any class that extends CommonAppBasewithout explicit use new?

Typically, there will be only one class definition for each PHP file. And adding new RandomName()to the end of all the files is what I would like to eliminate. An expanding class does not have a constructor; only the constructor is CommonAppBasecalled. CommonAppBase->__construct()launches the rest of the application execution.

Strange question, but it would be nice if someone knew the solution.

Edit

In addition to the following comment. The code that creates the instance will not be in the class file. The class file will be just that, I want some other code to be include('random.class.php')and create an instance of any class extension CommonAppBase.

For anyone who is not sure that after my hacker answer , I do what I want, but not in the safest way.

Thanks in advance, Aiden

(btw, my version of PHP is 5.3.2). Please indicate version restrictions with any answer.

The answers

The following can be added to the file (via php.ini or with Apache): automatically run the class of the specific parent class.

At first (thanks dnagirl)

$ca = get_declared_classes();
foreach($ca as $c){
    if(is_subclass_of($c, 'MyBaseClass')){
        $inst = new $c();
    }
}

and (accepted answer as closest answer)

auto_loader();
function auto_loader() {
    // Get classes with parent MyBaseClass
    $classes = array_filter(get_declared_classes(), function($class){
        return get_parent_class($class) === 'MyBaseClass';
    });
    // Instantiate the first one
    if (isset($classes[0])) {
        $inst = new $classes[0];
    }
}
+5
source share
10 answers

, , , . .

auto_loader();
function auto_loader() {
    // Get classes with parent MyBaseClass
    $classes = array_filter(get_declared_classes(), function($class){
        return get_parent_class($class) === 'MyBaseClass';
    });
    // Instantiate the first one
    if (isset($classes[0])) {
        $inst = new $classes[0];
    }
}

. PHP 4, , array_filter, PHP 5.3.0.

+2

, new.

, , get_defined_classes() . (array_diff()) , . , , - .

. , random.class.php, , random, :

$classname = "random";

require "$classname.class.php";
$$classname = new $classname();  // Produces an object instance `$random`
+2

, , (, ), factory. , , , - ....

$obj = &MyFactory::getClass('mysql_database_class');

. , , .

+2

call_user_func_array ( ($ , '__ '), $);

+1

, ?

class CommonAppBase {}
class RandomName extends CommonAppBase {}
$klass = 'RandomName';
$instance = new $klass();
+1

, __autoload, new?

, new . , :

static function make(array $args=NULL){
  $class=static::who(); //note: static, NOT self
  $obj=new $class($args);

  //some test for whether or not $obj is acceptable
  return ($test) ? $obj : false;  
}

abstract static function who(){
  return __CLASS__;
}

RandomClass :

$classname= 'RandomClass';
$someargs=array(1,2,3);

if(!$newobj= $classname::make($someargs)) die('cannot make new object');
+1

, , ()

function auto_loader()
{
    $file = $_SERVER['SCRIPT_FILENAME']; // Some class file
    $cont = file_get_contents($file);
    $tokens = token_get_all($cont);
    for($i=0; $i<sizeof($tokens); $i++) {
        // Token = $token[$i][0]
        // Lexeme is $token[$i][1]
        if($tokens[$i][0]==T_EXTENDS && $tokens[$i+2][1]=="MyBaseClass"){
            // Get the lexeme of the class
            $class = $tokens[$i-2][1];
            break;
        }
    }
    $inst = new $class();
}
auto_loader();

Apache - . , , ($i-2) extends BaseAppClass.

, , , , . .

, http://www.foo.com/some_class_file.php, Apache/php.ini some_class_file.php . , URL- . MyDogBenjiClass.

2, dnagirl .

$ca = get_declared_classes();
foreach($ca as $c){
    if(is_subclass_of($c, 'MyBaseClass')){
        $inst = new $c();
    }
}
+1

geek, APD override_function include.

( ) .

0

Reflection ?

$cls = new ReflectionClass('ClassName');
$obj = $cls->newInstance();

.

0
  • URL- index.php
  • $_SERVER['REQUEST_URI'] ( - )
  • classname init ($main = new $file())

MVC - , :

  • mod_rewrite index.php, -
  • URL: http://example.com/foo/bar/baz $controller = 'foo', $action = 'bar', $param = 'baz'
  • controllers/foo.php
  • : $controllerInstance = new $controller()
  • : call_user_func_array(array($controllerInstance, 'do'.ucfirst($action)), array($param))
0

All Articles