When I try to resolve a dependecy class like this
app(MyClass::class);
it works perfectly, but what I actually need is to pass the name of the class like a string, something similar to this:
$className = 'MyClass';
app($className::class);
I tried using reflection and every possible way but without success.
You have to pass full namespace of your class such as:
$className = "\\App\\MyClass";
app($className);
Related
Is it possible to reflect into a class without knowing the fully qualified class name?
e.g. instead of this...
(new ReflectionClass(\App\Modules\Companies\Models\Company::class));
...something like this?
(new ReflectionClass('Company'));
I have a couple functions that are used in multiple controllers in my Symfony project. Instead of copy and pasting them constantly, I'd like to have a separate php file (say functions.php) to store the functions.
I've tried the following:
1) include and require the php file with the functions, but it won't let me use $this->getDoctrine().
I've looked at the following posts for help, but it didn't solve the issue:
Symfony2: get Doctrine in a generic PHP class
Symfony2 Use Doctrine in Service Container
2) I've tried making it a class and having the functions as methods then doing:
$f = new Functions();
$variable = $f->myMethod();
But it returns:
The file was found but the class was not in it, the class name or namespace probably has a typo.
Thanks for any help. I really appreciate it.
UPDATE:
The generic class is Symfony/src/WikiRoster/MainBundle/Controller/DBFunctions.php
I just need to be able to use $this->getDoctrine() or the like somehow in there now. I've tried using the services.yml as suggested in the link above, but no cigar. Thanks!
The file was found but the class was not in it, the class name or
namespace probably has a typo.
This issue happens quite often actually. As the error mentions, there are 2 cases explaining why this error is returned:
If the namespace of your file is not the same as the path of your file
If the name of your file is not the same as the name you are using in your class
Example
Let's say you would like to use a GeneralClass in a different class like that:
use Acme\YourBundle\General\GenericClass
For this to work, GeneralClass needs to be in Acme/YourBundle/General/, and you need to:
use the correct namespace: namespace Acme\YourBundle\General
name your class with the same name: class GenericClass
You would have:
namespace Acme\YourBundle\General;
class GenericClass
{
public function __construct()
{
// some cool stuff
}
}
I just got stuck with the following problem. I'm trying to construct a class but it isn't working. When I'm constructing the class it uses namespaces and variables, because the variable has to be put behind a backslash (for the namespace) it gives an error.
Here's an example code of defining the class;
$className = 'Help';
$controller = new \controller\$className;
The class name is defined in a variable, in the example code above I've just set it to 'Help'. When constructing the class it has \controller\ in front of it, that's cause it's in that namespace. If I remove the namespace thing away so it just has;
$controller = new $className;
it does work. The problem is probably caused because there's a backslash in front of the variable which contains the class name to construct. I am not able to add use controller; in the beginning of the code because the file containing the class is loaded inside the constructor, and the class is immediately constructed after the file has been loaded. The use function could only be used in the beginning of your file, not inside a method, I think.
I hope someone could help me out,.
Thanks in advance,
Tim Visée
Your first example is invalid syntax. You would need instead something like:
$className = '\controller\Help';
$controller = new $className();
Set the fully qualified class name to a variable and use that to instantiate a new object:
$className = '\controller\Help';
$controller = new $className;
As the title says, when I instantiate a class I get this message :
Fatal error: Class 'Envato\RecursiveIteratorIterator' not found in C:\Users\rgr\Apache\htdocs\Roland Groza [ 3.0 ]\class\envato\envato.php on line 359
You can view the class here : Class ;
I'm instantiating from another file :
require("envato.php");
$test = new Envato\EnvatoAPIWrapper();
echo "User Vitals : ".$test->get_user_vitals("chaoscod3r")."<br>";
The class is wrapped with a namespace, so that might have something to do with it, but I wasn't sure since it's been a few years since I haven't coded PHP. Hopefully someone has an idea what is it that I'm doing wrong :)
To access non-namespaced classes like the internal classes of PHP and SPL inside of a namespace you have to use the fully qualified class name like this:
new \RecursiveIteratorIterator();
or import it explicitly at the beginning:
use \RecursiveIteratorIterator;
and then use it normally like you do.
Add a use statement at the top of your namespace...
use \RecursiveIteratorIterator;
If you don't then PHP expects RecursiveIteratorIterator to exist within your current namespace, rather than in the global namespace (indicated by the leading \)
I have this array:
$methodsOutput = array("MidUpperArmMuscleArea","ArmSpan_HeightEstimation","BMIBeforePregnancy",
"PregnancyTotalWeightGain","PregnancyWeeklyWeightGain","MS_CurrentBMI_BMIPregnancyWeeks",
"PregnancyDueDate","PregnancyWeeks","WeightEstimation","WHO_BMI_2000_IdealBMI",
"WHO_BMI_2000_IdealWeight","WHO_HeightAge_2006_IdealHeight","WHO_WeightAge_2006_IdealWeight");
and for each one I have the respective class (all included in file).
How can I instantiate dynamically this.
I try this:
foreach($methodsOutput as $method) {
$$method= new $method();
}
but get:
Fatal error: Class 'MidUpperArmMuscleArea' not found in...
Objectively, this looks like a really bad way of doing this..
Anyway, that's not what you want to hear.
The class 'MidUpperArmMuscleArea' must really not exist in that context, as your syntax is correct. Since you mention all these classes are in the same file, are you defining them after you are executing this code?
Do you have that class "MidUpperArmMuscleArea" ?
You could to use class_exists to test.