[SOLVED] create obj by name

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jakobdoppler
Forum Commoner
Posts: 46
Joined: Wed May 21, 2003 6:16 pm

[SOLVED] create obj by name

Post 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
User avatar
nigma
DevNet Resident
Posts: 1094
Joined: Sat Jan 25, 2003 1:49 am

Post 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();
jakobdoppler
Forum Commoner
Posts: 46
Joined: Wed May 21, 2003 6:16 pm

Post 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 :-)
Post Reply