Demonic Posted May 27, 2007 Share Posted May 27, 2007 alright I'm trying to see if there was an easier way to create a new Object using an array like so: $nbbext = ".php"; $act = array( 'config' ); $this->page = htmlspecialchars(trim($_GET['act'])); if(in_array($this->page,$act,true)){ include("class/".$this->page.$nbbext); echo $this->page; $setup = new $act[$this->page]; //$setup->run(); //$this->ATitle = $setup->title; //$this->ADesc = $setup->desc; } but I got an error Cannot instantiate non-existent class: There wasn't a problem including the file or anything its just weird why the object variable won't work. Can someone explain how I can get it to work? Quote Link to comment https://forums.phpfreaks.com/topic/53197-some-quick-oop-help/ Share on other sites More sharing options...
per1os Posted May 29, 2007 Share Posted May 29, 2007 You need an actual class definition for the class part to work. <?php class config { var $title; var $desc; function config() { } } $setup = new config(); $setup->title = "test title"; echo $setup->title; ?> What it seems like you want to do is illogical. The part of doing a new $act, is trying to create a new class called "config" when there is no such class called config. I am unsure of how to create objects on the fly or if that is even possible. But yea. You would have to have a class defined for that work as far as I know. Quote Link to comment https://forums.phpfreaks.com/topic/53197-some-quick-oop-help/#findComment-264152 Share on other sites More sharing options...
utexas_pjm Posted May 29, 2007 Share Posted May 29, 2007 What it seems like you want to do is illogical. The part of doing a new $act, is trying to create a new class called "config" when there is no such class called config. I am unsure of how to create objects on the fly or if that is even possible. But yea. You would have to have a class defined for that work as far as I know. With PHP you can instantiate object instances on fly. I've included a trivial example below: <?php class Foo { function speak() { echo 'Foo'; } } $objectName = 'Foo'; $instance = new $objectName(); $instance->speak(); ?> It seems to me that $act[$this->page] is null so you are trying to instantiate an instance of class: '' which of course does not exist. Try to echo out all of the values of the array before you instantiate them --there are probably some nulls mixed in. Best, Patrick Quote Link to comment https://forums.phpfreaks.com/topic/53197-some-quick-oop-help/#findComment-264241 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.