I'm new to PHP. When working with namespaces, I ran into a problem with RedBeanPHP.
I have php script "db.php":
namespace Fromtify\Database {
require_once('../libs/rb-mysql.php');
class Contoller
{
public function Connect()
{
R::setup(
#My database settings here...
);
if (!R::testConnection()) {
echo 'Cant connect to database';
exit;
}
}
}
}
My IDE(VSCode) tells me: "Undefined type 'Fromtify\Database\R"
How can I solve the problem?
Thank you in advance
When you use namespaces, PHP will assume that any class you load is under that namespace as well, unless you've imported it.
Example:
namespace Foo;
$bar = new Bar;
This will assume that Bar also is under the namespace Foo.
If the class you're using is under another namespace, or not in a namespace at all (the global namespace), you need to tell PHP which class to use by importing it. You do this with the use
namespace Foo;
use Bar;
$bar = new Bar;
So in your case, it should be:
namespace Fromtify\Database {
use R;
R:setup(...);
// The rest of your code
}
Side note!_
Unless you have multiple namespaces in the same file, which you usually don't have, there's not need for the syntax:
namespace Foo {
// your code
}
You can simpy do:
namespace Foo;
// Your code.
...which makes the code a bit cleaner.
You can read more about defining namespaces and using namespaces in the manual
Related
I've got the following problem:
This is my super basic class:
class A
{
function foo()
{
echo "bar";
}
}
Now in front of the class declaration I use the following code:
$a = new A();
$a->foo();
When I open the php file in the browser, the output is "bar". Fine!
Now I want to do the same thing in another file.
directly in the first place I declare the following namespace:
namespace model\dbAction;
This is the path where my file with the class above is located.
So in another php file I do the following:
$a = new \model\dbAction\A();
$a->foo();
But I don't get any output and other code after that won't run so it looks like it breaks directly after the instancing of the class.
Any ideas why instancing the class in another file is not working?
Thanks!
Full code first php file:
<?php
namespace model\dbAction;
class A
{
function foo()
{
echo "bar";
}
}
Full code of the second file (which I call in the browser):
$a = new \model\dbAction\A();
$a->foo();
You still need to include the file -- providing the namespace itself will not include the file for you... unless you're using an autoloader. See: How do I use PHP namespaces with autoload?
If you try using class_exists() inside a method of a class in PHP you have to specify the full name of the class--the current namespace is not respected. For example if my class is:
<?
namespace Foo;
class Bar{
public function doesBooClassExist(){
return class_exists('Boo');
}
}
And Boo is a class (which properly autoloads) and looks like this
namespace Foo;
class Boo{
// stuff in here
}
if I try:
$bar = new Bar();
$success = $bar->doesBooClassExist();
var_dump($success);
you'll get a false... is there an alternative way to do this without having to explicitly specify the full class name ( i.e. class_exits('Foo\Boo') )?
Prior to 5.5, the best way to do this is to always use the fully qualified class name:
public function doesBooClassExist() {
return class_exists('Foo\Boo');
}
It's not difficult, and it makes it absolutely clear what you're referring to. Remember, you should be going for readability. Namespace imports are handy for writing, but make reading confusing (because you need to keep in mind the current namespace and any imports when reading code).
However, in 5.5, there's a new construct coming:
public function doesBooClassExist() {
return class_exists(Boo::class);
}
The class pseudo magic constant can be put onto any identifier and it will return the fully qualified class name that it will resolve to.......
Example in file 1:
namespace A;
class Foo{
}
file 2:
use A\Foo;
do_stuff('A\Foo'); // <- need namespace here :(
Foo::someStaticMethod(); // <- namespace not required :D
Is there any way I can pass class names in function arguments like constants or something, so I don't need to prepend the namespace?
Update :)
When I know, that I need to pass the classnames of some classes around as string I'm used to create special class constant
namespace Foo\Bar;
class A {
const __NAMESPACE = __NAMESPACE__;
const __CLASS = __CLASS__;
}
Now you can reference the classname like
use Foo\Bar\A as Baz;
echo Baz::__CLASS;
With PHP5.5 this will be builtin
echo Baz::class;
Full-Qualified-Names (FQN) for namespaces always starts with a namespace separator
do_stuff('\A\Foo');
except (and thats the only exception) in use-statements, because there can only appear complete namespace identifiers, so for convenience you can omit it there.
However, a string is a string and where you use it as a class name is out of scope of the interpreter, so it lost the reference to the former use A\Foo-aliasing. With PHP5.5 you can write Foo::class, but I think thats not an option right now ;)
You could instantiate a new object, then call get_class() to get the fully qualified name for the class.
use A\Foo;
$foo = new Foo();
do_stuff(get_class($foo)); // get_class($foo) = '\A\Foo'
This means that the namespace of Foo is only defined by the use statement (ie. less code maintenance).
Or you can pass class reflection.
ReflectionClass
No, not without tracing the caller, as far as I know. The function you are calling must exists within the same namespace as the object you are trying to pass.
You might want to have a look at the debug_backtrace function if you require the namespace resolution. But this requires the file-paths to be translated into namespace resolutions or similar.
This is however possible: (I see Andrew has answered with the same type of solution.)
function doStuff ($obj)
{
$name = (is_object($obj))
? (new ReflectionClass(get_class($obj)))->getName()
: $obj;
// $name will now contain the fully qualified name
}
namespace Common;
class Test
{}
$testObj = new Test();
// This will work, but requires argument to be
// fully quialified or an instance of the object.
\doStuff($testObj);
\doStuff("\Common\Test");
From other posts, it appears that if you have namespaces defined and want to dynamically create an object in another namespace, you have to construct a string and use that in the new call. However, I'm getting a weird behavior. It appears that this method does not work going across namespaces.
User.php:
namespace application\models;
class User {
public function hello() {
echo "Hello from User!";
}
}
Controller.php:
namespace application\controllers;
use application\models;
require('User.php');
$userStr = 'models\\User';
//$userOne = new $userStr(); //Doesn't work. Gets a "Class 'models\User' not found" error
$userOne = new models\User(); //Works fine
$userStr = '\\application\\models\\User';
$userTwo = new $userStr(); //Works fine
$userOne->hello();
$userTwo->hello();
Any idea why when using a variable for the class name, I need to use the fully qualified namespace when it's in a variable, but hard coded, I can leverage the "use" command?
You can not import with use into variable classnames. That is a limitation of PHP.
See as well the related questions:
Expanding PHP namespace alias to full namespace string
Can't get constant from dynamic class using namespaces
Is there any way to temporary use a namespace ?
I'm using a library to create forms and it uses namespaces, the problem is that I usually want to create a form in the middle of a page, which is thus in the global namespace. Then if I want to call any function of this library I have to prefix everything with Namespace\
Isn't there any way in PHP to do something like this :
Blabla global namespace
strlen('test'); // 4
namespace Namespace
{
test();
}
More global PHP
And have it refer to Namespace\test ?
http://www.php.net/manual/en/language.namespaces.importing.php
<?php
namespace foo;
use My\Full\Classname as Another;
// this is the same as use My\Full\NSname as NSname
use My\Full\NSname;
// importing a global class
use ArrayObject;
$obj = new namespace\Another; // instantiates object of class foo\Another
$obj = new Another; // instantiates object of class My\Full\Classname
NSname\subns\func(); // calls function My\Full\NSname\subns\func
$a = new ArrayObject(array(1)); // instantiates object of class ArrayObject
// without the "use ArrayObject" we would instantiate an object of class foo\ArrayObject
?>
This is about the closest you can get - there's no way to change the default namespace temporarily.
I know this is an old question, and while the accepted answer answers the question as it was asked, I feel like what the OP is really asking is "Can I use items in the global namespace from another namespace. The answer here is a clear and simple yes.
Imagine two classes (one in the global namespace, another in its own:
ClassInGlobal.php
<?php
class ClassInGlobal
{
public static function doStuff()
{
echo 'I do some stuff';
}
}
ClassInNamespace.php
<?php
namespace App\Classes;
class ClassInNamespace
{
public function callDoStuff()
{
\ClassInGlobal::doStuff();
}
}
The above executes fine. All that is required is a leading slash to specify the fully qualified global namespace. Additionally, you could add a use ClassInGlobal declaration right after the namespace declaration and omit the leading slash.
This might translate into the original question by abstracting the namespaced functions into a class Then you could modify the OP's code slightly to acheive this:
require './Namespace/Utilities.php';
Blabla global namespace
strlen('test'); // 4
\Namespace\Utilities::test();
More global PHP
Hope that helps someone coming here looking for that.