Page 1 of 1

[SOLVED] create obj by name

Posted: Mon Jul 26, 2004 9:51 am
by jakobdoppler
Hi

Depending on a $_GET parameter, called module, I get to know which php file i have to include and execute. Every file contains a special module class to work with. Since the $_GET param is of the same name as the include file I can easily include it by

Code: Select all

<?php
require($_GET['module'].".php")
?>
The class in the file has the same name also but how can I dynamically instantiate a PHP class with just knowing its name ?

Code: Select all

<?php
$module = new $_GET['module'].".php";
$_GET['module']->doMethod();
?>
will not work (of course)

Any Idea , how to instantiate objects by just knowing their name ?

regards _yak

Posted: Mon Jul 26, 2004 10:05 am
by nigma

Code: Select all

// assuming that $_GET['module'] is the name of the class, then:
$obj = new $_GET['module'];

// replace method() with the name of the method you want to call
$obj->method();

Posted: Mon Jul 26, 2004 10:10 am
by jakobdoppler
Ok besides the fact, that this one does work , I made a code mistake (by adding this .php) and being totally out of mind today, I'd like to apologise for asking stupid question before thinking about that.

Nevertheless , thankx _yak :-)