Instantiate a Class from a Variable in PHP

instantiate a class from a variable in PHP?

Put the classname into a variable first:

$classname=$var.'Class';

$bar=new $classname("xyz");

This is often the sort of thing you'll see wrapped up in a Factory pattern.

See Namespaces and dynamic language features for further details.

Instantiate Class and Declare Variables in One Line

If you want to feed your own values, use your constructor to setup the properties in the arguments:

class Gallery 
{
public $image;
public $text;
public $heading;
public $link;

public function __construct($image, $text, $heading, $link) // arguments
{
$this->image = $image;
$this->text = $text;
$this->heading = $heading;
$this->link = $link;
}

public function Item()
// rest of your codes

So that when you instantiate, just fill up the arguments:

$gallery = new Gallery('img/test.jpg', 'test', 'text', 'link#');
$gallery->Item(); // echoes the html markup,

PHP how to dynamically instantiate a class

The new operator accepts either a class name identifier, or a variable containing a class name, but not a mixture of them.

Since a part of your fully qualified class name is unknown (dynamic), you should put all the parts into a string variable:

$class_name = 'A';
$namespace = '\\App';
$fully_qualified_class_name = "$namespace\\$class_name";
$var = new $fully_qualified_class_name;

How to initialize variable in constructor in PHP

Try this (note the echo lines):

class Animal {
public $_type;
public $_breed;

public function __construct ($t, $b) {
echo "i have initialized<br/>";

$this->_type = $t;
$this->_breed = $b;

//You have to use '$this' keyword to access
//class attibutes:
echo "type is " . $this->_type . "<br/>";
echo "breed is " . $this->_breed . "<br/>";
}

public function __destruct () {
echo "i am dying";
}
}

Using variable with string for new class initialization

When using string based creation, you must use the fully qualified namespace of the class regardless of the local namespace.

use My\Stuff;
$o = new Stuff();
$name = ‘My\Stuff’;
$o = new $name();
$name = Stuff::class;
$o = new $name();

Instantiating a PHP class using a variable is not working

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.

Creating PHP class instance with a string

Yes, you can!

$str = 'One';
$class = 'Class'.$str;
$object = new $class();

When using namespaces, supply the fully qualified name:

$class = '\Foo\Bar\MyClass'; 
$instance = new $class();

Other cool stuff you can do in php are:

Variable variables:

$personCount = 123;
$varname = 'personCount';
echo $$varname; // echo's 123

And variable functions & methods.

$func = 'my_function';
$func('param1'); // calls my_function('param1');

$method = 'doStuff';
$object = new MyClass();
$object->$method(); // calls the MyClass->doStuff() method.

How to use variable name to call a class?

Without ReflectionClass:

$instance = new $className();

With ReflectionClass: use the ReflectionClass::newInstance() method:

$instance = (new \ReflectionClass($className))->newInstance();

PHP: How to instantiate a class with arguments from within another class

you need Reflection http://php.net/manual/en/class.reflectionclass.php

if(count($args) == 0)
$obj = new $className;
else {
$r = new ReflectionClass($className);
$obj = $r->newInstanceArgs($args);
}


Related Topics



Leave a reply



Submit