'use' directive while using ReflectionClass in symfony app - php

I have a class in my symfony 3 app with a method that should instantiate a Model class dynamically using the name that's passed in to the function, i.e.:
static function getInstance($modelName){
use $modelName;
$r = new \ReflectionClass($modelName);
return $r->newInstanceArgs();
}
But there's a syntax error on the use directive.
I've tried adding specific use statements for every class at the top of the file; and I've also tried using require with a fully qualified path to the corresponding PHP file - but neither approach has worked.
Please advise how to correctly do this.

What about this? use qualified class name
static function getInstance($modelName){
$r = new \ReflectionClass('AppBundle\Entity\\' . $modelName);
return $r->newInstanceArgs();
}

Also, per PHP USE Documentation:
Scoping rules for importing
The use keyword must be declared in the outermost scope of a file (the global scope) or inside namespace declarations. This is because the importing is done at compile time and not runtime, so it cannot be block scoped. The following example will show an illegal use of the use keyword:
Example #5 Illegal importing rule
namespace Languages;
function toGreenlandic() {
use Languages\Danish;
}

Related

php use all classes with 'use' inside a namespace | not 'include' but 'use <namespace>' | vanilla php

I have a class that uses every single class inside a Controllers folder. To be able to use them i need to do
use <namespace>\<class>;
But that gets repetitive if you have loads of classes and takes a lot of space.
I know in c# you can include ALL classes inside a specific folder like
using Controllers
So all the classes will be loaded with the 'Controllers' and wont need to be repeated for 10 times for 10 different classes inside this 'Controllers' namespace.
But how to do that with Php?
I tried looping glob
foreach (glob("Controllers/*.php") as $filename) {
use $filename;
}
But throws an error for using 'use' and that is expected since this is a wrong way to use it. Al-tho switching 'use' with 'include' works but then does not find such $filename. I need to use 'use' not 'include'
Is this even possible or am I just wasting time trying to do this without the use of any 3rd party lib or framework (vanilla php)
You can use namespace in PHP:
let's assume I have
App\Namespace1\Foo
App\Namespace1\Bar
App\Namespace2\Baz
namespace App\Namespace2;
use App\Namespace1;
class Baz
{
public function __construct(Namespace1\Foo $foo)
{
new Namespace1\Bar($foo);
}
}
Because use can not be used inside function as of PHP manual
The use keyword must be declared in the outermost scope of a file (the global scope)
I think your best bet would be to use PHP7 group use statement
use App\Foo{
Class1,
Class2,
Class3
}
As side note, why you need to use all controllers? Maybe there is some logic that can be separated to different files?
Your issue would be solved if all controllers and current class would be under same namespace, then class lookup first checks in current namespace.
If you are using dynamic class loading, then you can exclude use and call class directly because:
use App\Foo\Class1;
...
new Class1();
is same as
new \App\Foo\Class1();
When doing dynamic loading:
$className = 'Class1'; // E.g. resolved by some routing
$fullClassName = "\\App\\Foo\\{$className}";
$instance = new $fullClassName();

Can not run Laravel app in Mac OSX [duplicate]

Getting this error when I put use Blog; at the top.
Warning: The use statement with non-compound name 'Blog' has no effect
in...
Blog is my namespace in which I have 3 classes: Article, List and Category and a few functions.
If I change my statememnt to use Blog\Article; then it works...
Can't I just specify the namespaces I want to use? Do I need to provide classes?
What if I have functions within that namespaces? When I call them outside of the namespace, I'm forced to prepend \Blog\ to each one's name...
PHP's use isn't the same as C++'s using namespace; it allows you to define an alias, not to "import" a namespace and thus henceforth omit the namespace qualifier altogether.
So, you could do:
use Blog\Article as BA;
... to shorten it, but you cannot get rid of it entirely.
Consequently, use Blog is useless, but I believe you could write:
use \ReallyLongNSName as RLNN;
Note that you must use a leading \ here to force the parser into knowing that ReallyLongNSName is fully-qualified. This isn't true for Blog\Article, which is obviously already a chain of namespaces:
Note that for namespaced names (fully qualified namespace names containing namespace separator, such as Foo\Bar as opposed to global names that do not, such as FooBar), the leading backslash is unnecessary and not recommended, as import names must be fully qualified, and are not processed relative to the current namespace.
http://php.net/manual/en/language.namespaces.importing.php
Since this question appears as the first result on Google for this error I will state how I fixed it.
Basically if you have a framework, say like Yii2 you will be used to having to do declare classes like:
use Yii;
use yii\db\WhatEver;
class AwesomeNewClass extends WhatEver
{
}
You will get this error on Use Yii since this class has no namespace.
Since this class has no namespace it automatically inherits the global symbol table and so does not need things like this defining, just remove it.
The use statement in PHP is really just a convenience to alias a long namespace into something that may be a little easier to read. It doesn't actually include any files or do anything else, that effects your development, besides providing convenience. Since, Blog isn't aliased as anything you aren't gaining any of the convenience. I could imagine you could do something like
use \Blog as B;
And that may even work. (It could be argued you actually lose convenience here by obscuring but that's not what the question is about) Because you're actually aliasing the Blog namespace to something else. Using Blog\Article works because, according to the docs:
// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;
So your snippet would be equivalent to:
use Blog\Article as Article;
if you don't want to use 'as' syntax like
use \Blog as B;
define a namespace for the file
namespace anyname;
use Blog
The error "The use statement ... has no effect..." also pops up if you try to use a trait before a class definition.
use My_trait; // should not be here
class My_class{
// use My_trait; should be here instead
}
Perhaps it's something like
namespace Path\To\Your\Namespace\Blog;
use Blog; // Redundant
class Post {
public $linkedArticle;
public function __construct($article = null)
{
$this->linkedArticle = $article ?? new Blog\Article();
}
}
Blog is already available, because that's the namespace you're in, so you can use new Blog\Article(); without use Blog; at the top. That's exactly what the error tells you - the added line has no effect.
Pointless:
use SingleNonNestedClassThatIsAlreadyPresentInTheCurrentNamespace;
Useful:
use SingleNonNestedClassThatIsAlreadyPresentInTheCurrentNamespace as Phew;
If you on the other hand wish to use new Article() then you can do it like this.
namespace Path\To\Your\Namespace\Blog;
use Blog\Article; // Equivalent to "use Blog\Article as Article;"
class Post {
public $linkedArticle;
public function __construct($article = null)
{
$this->linkedArticle = $article ?? new Article();
}
}
In practice you'd do something like
// Fairly separated domains
use Some\TooLong\Namespace\App\User;
use Some\TooLong\Namespace\App\Ecommerce;
use Some\TooLong\Namespace\App\Auth;
but not necessarilly
// Two tools in same domain
use Some\TooLong\Namespace\App\Ecommerce\Cart;
use Some\TooLong\Namespace\App\Ecommerce\Checkout;
as well. I'm positive there are better examples than this ;)

Dynamic namespaced class with alias

SO,
I have an issue with dynamic object creation using namespaces.
Here's namespace code:
namespace Foo
{
class Bar
{
}
}
Now, I'm trying to create object of class Bar with:
include('namespace.php');
$sName = 'Bar';
$sClass = '\\Foo\\'.$sName;
$rObj = new $sClass; //correct object
and everything going well with that. But, now I want to use alias and doing something like:
include('namespace.php');
use Foo as Baz;
$sName = 'Bar';
$sClass0= '\\Foo\\'.$sName;
$sClass1= '\\Baz\\'.$sName;
$rObj = new $sClass0; //correct object
$rObj = new $sClass1; //Fatal error
And I'm unable to instantiate an object such way (and accessing via full name still works well).
So, my question is - is it possible to access the class via alias somehow, and, if yes, how? I've also tried to access when using $sClass1='Baz\\'.$sName - no success. Also, I've checked declared classes via get_declared_classes() function, it shows that I have only \Foo\Bar class (no reference to an alias).
I'm not sure if it matters, but I'm using PHP 5.5 version.
Only the parser uses your namespace aliases to canonicalize the class references inside each of your files.
In other words, it doesn't introduce some kind of global alias that other code can use. Once your script has been parsed, the alias is no longer used.
This behaviour is also described in the manual:
Importing is performed at compile-time, and so does not affect dynamic class, function or constant names.

How does the keyword "use" work in PHP and can I import classes with it?

I have a file with a class Resp. The path is:
C:\xampp\htdocs\One\Classes\Resp.php
And I have an index.php file in this directory:
C:\xampp\htdocs\Two\Http\index.php
In this index.php file I want to instantiate a class Resp.
$a = new Resp();
I know I can use require or include keywords to include the file with a class:
require("One\Classes\Resp.php"); // I've set the include_path correctly already ";C:\xampp\htdocs". It works.
$a = new Resp();
But I want to import classes without using require or include. I'm trying to understand how use keyword works. I tried theses steps but nothing works:
use One\Classes\Resp;
use xampp\htdocs\One\Classes\Resp;
use htdocs\One\Classes\Resp;
use One\Classes;
use htdocs\One\Classes; /* nothing works */
$a = new Resp();
It says:
Fatal error: Class 'One\Classes\Resp' not found in C:\xampp\htdocs\Two\Http\index.php
How does the keyword use work? Can I use it to import classes?
No, you can not import a class with the use keyword. You have to use include/require statement. Even if you use a PHP auto loader, still autoloader will have to use either include or require internally.
The Purpose of use keyword:
Consider a case where you have two classes with the same name; you'll find it strange, but when you are working with a big MVC structure, it happens. So if you have two classes with the same name, put them in different namespaces. Now consider when your auto loader is loading both classes (does by require), and you are about to use object of class. In this case, the compiler will get confused which class object to load among two. To help the compiler make a decision, you can use the use statement so that it can make a decision which one is going to be used on.
Nowadays major frameworks do use include or require via composer and psr
1) composer
2) PSR-4 autoloader
Going through them may help you further.
You can also use an alias to address an exact class. Suppose you've got two classes with the same name, say Mailer with two different namespaces:
namespace SMTP;
class Mailer{}
and
namespace Mailgun;
class Mailer{}
And if you want to use both Mailer classes at the same time then you can use an alias.
use SMTP\Mailer as SMTPMailer;
use Mailgun\Mailer as MailgunMailer;
Later in your code if you want to access those class objects then you can do the following:
$smtp_mailer = new SMTPMailer;
$mailgun_mailer = new MailgunMailer;
It will reference the original class.
Some may get confused that then of there are not Similar class names then there is no use of use keyword. Well, you can use __autoload($class) function which will be called automatically when use statement gets executed with the class to be used as an argument and this can help you to load the class at run-time on the fly as and when needed.
Refer this answer to know more about class autoloader.
use doesn't include anything. It just imports the specified namespace (or class) to the current scope
If you want the classes to be autoloaded - read about autoloading
Don’t overthink what a Namespace is.
Namespace is basically just a Class prefix (like directory in Operating System) to ensure the Class path uniqueness.
Also just to make things clear, the use statement is not doing anything only aliasing your Namespaces so you can use shortcuts or include Classes with the same name but different Namespace in the same file.
E.g:
// You can do this at the top of your Class
use Symfony\Component\Debug\Debug;
if ($_SERVER['APP_DEBUG']) {
// So you can utilize the Debug class it in an elegant way
Debug::enable();
// Instead of this ugly one
// \Symfony\Component\Debug\Debug::enable();
}
If you want to know how PHP Namespaces and autoloading (the old way as well as the new way with Composer) works, you can read the blog post I just wrote on this topic: https://enterprise-level-php.com/2017/12/25/the-magic-behind-autoloading-php-files-using-composer.html
You'll have to include/require the class anyway, otherwise PHP won't know about the namespace.
You don't necessary have to do it in the same file though. You can do it in a bootstrap file for example. (or use an autoloader, but that's not the topic actually)
The issue is most likely you will need to use an auto loader that will take the name of the class (break by '\' in this case) and map it to a directory structure.
You can check out this article on the autoloading functionality of PHP. There are many implementations of this type of functionality in frameworks already.
I've actually implemented one before. Here's a link.
I agree with Green, Symfony needs namespace, so why not use them ?
This is how an example controller class starts:
namespace Acme\DemoBundle\Controller;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
class WelcomeController extends Controller { ... }
Can I use it to import classes?
You can't do it like that besides the examples above. You can also use the keyword use inside classes to import traits, like this:
trait Stuff {
private $baz = 'baz';
public function bar() {
return $this->baz;
}
}
class Cls {
use Stuff; // import traits like this
}
$foo = new Cls;
echo $foo->bar(); // spits out 'baz'
The use keyword is for aliasing in PHP and it does not import the classes. This really helps
1) When you have classes with same name in different namespaces
2) Avoid using really long class name over and over again.
Using the keyword "use" is for shortening namespace literals. You can use both with aliasing and without it. Without aliasing you must use last part of full namespace.
<?php
use foo\bar\lastPart;
$obj=new lastPart\AnyClass(); //If there's not the line above, a fatal error will be encountered.
?>
Namespace is use to define the path to a specific file containing a class e.g.
namespace album/className;
class className{
//enter class properties and methods here
}
You can then include this specific class into another php file by using the keyword "use" like this:
use album/className;
class album extends classname {
//enter class properties and methods
}
NOTE: Do not use the path to the file containing the class to be implements, extends of use to instantiate an object but only use the namespace.

Troubleshooting "The use statement with non-compound name ... has no effect"

Getting this error when I put use Blog; at the top.
Warning: The use statement with non-compound name 'Blog' has no effect
in...
Blog is my namespace in which I have 3 classes: Article, List and Category and a few functions.
If I change my statememnt to use Blog\Article; then it works...
Can't I just specify the namespaces I want to use? Do I need to provide classes?
What if I have functions within that namespaces? When I call them outside of the namespace, I'm forced to prepend \Blog\ to each one's name...
PHP's use isn't the same as C++'s using namespace; it allows you to define an alias, not to "import" a namespace and thus henceforth omit the namespace qualifier altogether.
So, you could do:
use Blog\Article as BA;
... to shorten it, but you cannot get rid of it entirely.
Consequently, use Blog is useless, but I believe you could write:
use \ReallyLongNSName as RLNN;
Note that you must use a leading \ here to force the parser into knowing that ReallyLongNSName is fully-qualified. This isn't true for Blog\Article, which is obviously already a chain of namespaces:
Note that for namespaced names (fully qualified namespace names containing namespace separator, such as Foo\Bar as opposed to global names that do not, such as FooBar), the leading backslash is unnecessary and not recommended, as import names must be fully qualified, and are not processed relative to the current namespace.
http://php.net/manual/en/language.namespaces.importing.php
Since this question appears as the first result on Google for this error I will state how I fixed it.
Basically if you have a framework, say like Yii2 you will be used to having to do declare classes like:
use Yii;
use yii\db\WhatEver;
class AwesomeNewClass extends WhatEver
{
}
You will get this error on Use Yii since this class has no namespace.
Since this class has no namespace it automatically inherits the global symbol table and so does not need things like this defining, just remove it.
The use statement in PHP is really just a convenience to alias a long namespace into something that may be a little easier to read. It doesn't actually include any files or do anything else, that effects your development, besides providing convenience. Since, Blog isn't aliased as anything you aren't gaining any of the convenience. I could imagine you could do something like
use \Blog as B;
And that may even work. (It could be argued you actually lose convenience here by obscuring but that's not what the question is about) Because you're actually aliasing the Blog namespace to something else. Using Blog\Article works because, according to the docs:
// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;
So your snippet would be equivalent to:
use Blog\Article as Article;
if you don't want to use 'as' syntax like
use \Blog as B;
define a namespace for the file
namespace anyname;
use Blog
The error "The use statement ... has no effect..." also pops up if you try to use a trait before a class definition.
use My_trait; // should not be here
class My_class{
// use My_trait; should be here instead
}
Perhaps it's something like
namespace Path\To\Your\Namespace\Blog;
use Blog; // Redundant
class Post {
public $linkedArticle;
public function __construct($article = null)
{
$this->linkedArticle = $article ?? new Blog\Article();
}
}
Blog is already available, because that's the namespace you're in, so you can use new Blog\Article(); without use Blog; at the top. That's exactly what the error tells you - the added line has no effect.
Pointless:
use SingleNonNestedClassThatIsAlreadyPresentInTheCurrentNamespace;
Useful:
use SingleNonNestedClassThatIsAlreadyPresentInTheCurrentNamespace as Phew;
If you on the other hand wish to use new Article() then you can do it like this.
namespace Path\To\Your\Namespace\Blog;
use Blog\Article; // Equivalent to "use Blog\Article as Article;"
class Post {
public $linkedArticle;
public function __construct($article = null)
{
$this->linkedArticle = $article ?? new Article();
}
}
In practice you'd do something like
// Fairly separated domains
use Some\TooLong\Namespace\App\User;
use Some\TooLong\Namespace\App\Ecommerce;
use Some\TooLong\Namespace\App\Auth;
but not necessarilly
// Two tools in same domain
use Some\TooLong\Namespace\App\Ecommerce\Cart;
use Some\TooLong\Namespace\App\Ecommerce\Checkout;
as well. I'm positive there are better examples than this ;)

Categories