Using a variable containing the name of a class then creating a new instance of that class, I have a question on the way I call the class in the variable.
My code works:
use AppBundle\Entity\someEntity;
$entityName = "AppBundle\Entity\someEntity";
$instance = new $entityName();
Now, what I would like to do is change the $entityName variable for something more simple like $entityName="someEntity";
Anyone have any idea on how to do that?
I am using PHP5.5
Thanks
Related
I'm trying to figure out if this is a logic issue on my part or just a lack of knowledge.
I have a static method ConnectionFactory::getConnectionInterface($config['type']), which returns a string, a class name. That class has a method on it, createConnection. I'm trying to figure out if I can do it all in one line or not. I tried various things like
new {ConnectionFactory::getConnectionInterface($config['type'])}()->createConnection();
Switching the {} for (), adding them around the whole new, etc. I feel like I get closer in some parts, further in others.
I know I could just have the factory return a new instance of the object (and from my understanding, that may be the right way to do it?), but I'm hoping to figure out how I can write this code, or if I can't.
You need to wrap the new object in ()
(new ConnectionFactory::getConnectionInterface($config['type'])())->createConnection();
Alternatively you could return an instance of the class instead of the class name.
public static function getConnectionInterface($type) {
// generate class name $class
return new $class()
}
Then use just use that object instead of creating a new instance when you call it.
$connection = ConnectionFactory::getConnectionInterface($config['type'])->createConnection()
I'm working on a PHP class that will fetch another class and somehow return it, obviously constructors can't return values so I was looking at passing by reference and this is what I got:
<?php
class Example {
public function __construct($name,&$var){
//This registers the class in my system (I know this part works)
IceTray::$Registry->registerLibrary($name);
//this fetches the object of the registered class above (I know it works)
$var = IceTray::$Registry->Libraries->$name;
}
Am I passing by reference wrong? Because when I use this in my project:
$test = 0;
$lib = new Example('ClassName', $test);
$test->testing();
$Test is the variable I wish to store the object in, the first argument of the constructor is the name of the class to register and assign to the variable passed by reference which is the second argument. The next line is called a method inside the requested class name, but it's not working.
No errors or anything, again I'm new to the passing by reference concept maybe I'm doing something wrong. Any help is very much appreciated, thanks in advance!
class Example {
public $var = null;
public function __construct($name){
//This registers the class in my system (I know this part works)
IceTray::$Registry->registerLibrary($name);
//this fetches the object of the registered class above (I know it works)
$this->var = IceTray::$Registry->Libraries->$name;
}
}
$lib = new Example('ClassName');
$lib->var->testing();
Why hurt yourself and not use calls properties to return what you need?
Can someone please explain what it means to have a class instance within the argument of another class instance:
$controller = new controllerObject(new dependenciesObject());
I understand the basics of classes and class instances but have never seen code like the above before. What does it mean? The controllerObect is a class so what does it mean to pass it an object instance in the argument?
All this means is that the controllerObject constructor accepts an object of type dependenciesObject.
You instantiate a dependenciesObject and use it as an argument to instantiate a controllerObject, which is assigned to $controller. If it helps you understand, this is equivalent to:
$dependency = new dependenciesObject();
$controller = new controllerObject($dependency);
unset($dependency);
What you're doing is assigning $controller an instance of controllerObject.
As you said controllerObject is a class with a constructor that needs a reference to an object of :dependenciesObject type.
class controllerObject
{
function __construct(dependanciesObject $dependancies)
{
//etc
}
}
It just means that the controllerObject gets another object passed to its constructor. By doing it like this - instead of instantiating a dependenciesObject in a separate variable - you will not create a global variable for the passed object, keeping the number of global variables to a minimum.
An example would be to pass a database object to the controllerObject if the controllerObject cannot function without it.
Same as passing any other argument to a constructor.. new controllerObject(5) for instance.
If it helps understanding, use a temporary variable:
$dependencies = new dependenciesObject();
$controller = new controllerObject($dependencies);
I want to create a new object using this
$procedure = new ${$s.'\\'.$p};
It doesn't work. Why isn't this possible?
Why don't you
$name = "$s\\$p";
$procedure = new $name;
?
Also ${$s.'\\'.$p} means a variable, with a variable name that is clearly not good. If you are, and I think you are, trying to get something like an instance of Namespace\Class you should try with the code below.
I think that the {} shortcut only works with this syntax ${} which is clearly referring to a variable. So you cannot use it for instantiating new objects.
I came across some weird behavior today around creating an object from the return value of a getter method.
Firstly I tried this
$productMapper = new $this->getDbMapper();
Which gives me an undefined property: $getDbMapper error.
From what it looks like, it's trying to access $getDbMapper as class property and then using the () for the class instantiation rather than as the method (?).
I also have this issue in a another section of code with where the class takes a constructor argument.
Would this be a good time to look at some kind of factory pattern over the top or am I just missing something?
I found a fix, which I'm not particularly happy with
$mapper = $this->getDbMapper();
$productMapper = new $mapper();
// With a class constructor
$form = $this->getForm();
$form = new $form($productData);