Jump to content

Some quick OOP help


Demonic

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/53197-some-quick-oop-help/
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/53197-some-quick-oop-help/#findComment-264152
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/53197-some-quick-oop-help/#findComment-264241
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.