generate class name? - php

how can you do something like this?
require_once 'class.Table_'.$table.'.php';
$class = new Table_$table();

$className = $var.'someString'.$var2;
$obj = new $className();
In your case
$className = 'Table_'.$table;
$obj = new $className();

Pretty much the way you did it.
The variable has to be the whole class name.
So:
<?php
$clsName = "Table_a";
require_once "class.{$clsName}.php";
$class = new $clsName();
?>
This is supported since PHP 5.2.

Related

new class from variable

I've got a problem. I'm using Laravel 5.4 and on initialization
$class = (string)$module->controller.'Controller';
$class = new $class();
$class->startModule($module->title,$request);
I get a response from server FatalErrorException in ModulesController.php line 29:
Class 'FileManagerController' not found
image
but on manual call it works fine
$class = new FileManagerController();
$class->startModule($module->title,$request)
Plese tell me what's the problem?
First require class and then create instance:
$class = (string)$module->controller.'Controller';
require_once $class . '.php';
$class = new $class();
$class->startModule($module->title,$request);
I've got solution
$class = (string)$module->controller.'Controller';
app('App\Http\Controllers\\'.$class)->startModule($module->title,$request);

Can I use a string variable to initialize a class in PHP?

I'd like to use a string variable to initialize an object. Is something like this possible?
$class = "MyClass";
$x = new $class();
return $x;
Edit: Ha, so when I tried to test this and it didn't work I had a syntax error somewhere else in my script. Apparently this works just fine. Neat.
Yes. Its possible in PHP.
$className = 'MyClass';
$object = new $className;
Attaching PHP documentation snippet on new operator

Instantiating a PHP class using a variable is not working

This is working:
$x = new classname();
This is not working:
$class = "classname";
$x = new $class();
The error I get is "Class classname not found". PHP version is 5.4.22. Any ideas? As far as I have researched into this topic this is exactly what you need to do in order to instantiate a class using a variable.
My actual testcode (copy+paste), $build = 1:
//include the update file
$class="db_update_" . str_pad($build, 4, '0', STR_PAD_LEFT);
require_once(__ROOT__ . "/dbupdates/" . $class . ".php");
$x = new db_update_0001();
$xyz="db_update_0001";
$x = new $xyz();
The class definition:
namespace dbupdates;
require_once("db_update.php");
class db_update_0001 extends db_update
{
...
}
I just found out that my editor added
use dbupdates\db_update_0001;
to the file. So that explains why "new db_update_0001();" is working. What i want to achieve is that I dynamically include database updates which are stored in files like dbupdates/db_update_0001.php
Regards,
Alex
You have to use the full qualified class name. Which is namespace\classname. So in your case the code should be:
$x = new db_update_0001();
$xyz="dbupdates\db_update_0001";
$x = new $xyz();
The use statement is useless if you like to instantiate a class by using a variable as classname.
Try this
<?php
$className = yourClassName();
$x = new $className;
?>

php create objects from list of class files

I have directory CLASSES with files in my project.
../classes/class.system.php
../classes/class.database.php
...
I pull out every class and include it to main index.php with this code:
// load classes
foreach (glob("classes/class.*.php") as $filename) {
require_once $filename;
}
and then I create (manually write) objects for example:
$system = new System();
$database = new Database();
...
Q: How can I automatically generate object for each class from list of files in directory CLASSES without writing them?
Thank you for your answers and code.
EDIT:
My working solution:
// load classes
foreach (glob("classes/class.*.php") as $filename) {
require_once $filename;
$t = explode(".",$filename);
$obj = strtolower($t[1]);
$class = ucfirst($t[1]);
${$obj} = new $class();
}
IF you follow a typical pattern, while creating those files like
class.<classname>.php
Then
foreach (glob("classes/class.*.php") as $filename) {
require_once $filename;
$t = explode(".",$filename);
${strtolower($t[1])}= new ucfirst($t[1])(); // automatically create the object
}
I don't think loading all classes with glob and including them if you need or not is efficient or good idea performance wise. What if you have 500 different classes ???
Why don't you take advantage of PHP auto loading see http://php.net/manual/en/language.oop5.autoload.php which would only load the class you need
Example
function __autoload($className) {
require_once "classes/class." . $className . ".php";
}
$system = new System();
$database = new Database();
// load classes and create objects
foreach (glob("classes/class.*.php") as $filename) {
require_once $filename;
$t = explode(".",$filename);
$obj = strtolower($t[1]);
$class = ucfirst($t[1]);
// create object for every class
${$obj} = new $class();
}

PHP: how to make new object of class with name stored in member string?

So I want to do something like this:
$ob = new $this->other_class_name;
but it fails. How can I do it without storing other_class_name in local variable?
Save the name in another variable:
$class = $this->other_class_name;
$ob = new $class;
Try this:
$class = get_class($this->other_class_name);
$ob = new $class;

Categories