Using variable names to instantiate a class

I'm trying to do something like this:

$controller = new $controller . 'Controller';

Which would create a new instance of the PagesController class if $controller was Pages. I'm getting this error, however:

Fatal error: Class 'Pages' not found in C:\xampp\htdocs\test\application\app.php on line 25

The class is named PagesController.

I've seen this done in PHP before, but I can't find any documentation on it.

Answers


Do this

$controller = $controller . 'Controller';
$controller = new $controller();

Need Your Help

.NET regex into PHP - Issue grabbing dollar amount

php regex

I am not very familiar in PHP and am kind of stuck grabbing a dollar amount from a sequence of text.

date as SEO optimization tool

php html date seo

Consider the following PHP code for printing current date:

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.