Namespacing forces my class to be unfound - php

namespace foo;
class foo{
}
$foo = new foo();
If I removed the namespace, the class works just fine, if the namespace is there, I get class foo unfound error. What is the reason for this?

If your class is namespaced, then you need to reference that namespace when instantiating (assuming you're in the global namespace when you instantiate).
$foo = new foo\foo();
That's the whole point. You can have multiple foo classes in different namespaces.
namespace foo;
class foo {}
And then...
namespace bar;
class foo {}
And now...
$foo1 = new foo\foo();
$foo2 = new bar\foo();
Read up on how namespaces work: http://it2.php.net/namespaces

Related

PHP, execute public static function from $class

how to call public static function of class from different namespace in php. I have this code:
namespace x\y\z;
use x\y\z\h\Foo;
...
$classinstring = 'Foo';
$classinstring::getType();
and i got error that php can't find class Foo Fatal error: Uncaught Error: Class 'Foo' not found how i can do this?
To instantiate a class, you should use new.
$classinstring = new Foo();
Writing $classinstring = 'Foo' assigns $classinstring the string literal "Foo".
Namespacing is a shortcut to your classes. These two statements are equal:
namespace x\y\z;
use x\y\z\h\Foo;
$bar = new Foo();
and
$bar = new \x\y\z\h\Foo();
Also make sure you have your class names spelled exactly the same as the file name.
Static methods do not need to be instantiated to be used; you can call them directly from the class name.
Foo::someCustomMethod();
You are using getType() as an example, although this is a native PHP global function and cannot be called as a static method, unless you have defined your own getType() method within the class.
class Foo
{
public function getType()
{
echo 'This is my own function.';
}
public static function callAnywhere()
{
echo 'You don't have to make a new one to use one.';
}
}
This is good if you need to call class methods.
Foo::callAnywhere() // prints 'You don't have to make a new one to use one.';
$bar = new Foo();
$bar->getType(); // prints 'This is my own function.'
$other = new stdClass();
echo getType($other); // prints 'object';
Try this.
namespace x\y\z;
use x\y\z\h\Foo;
...
$classinstring = 'Foo';
$classinstring = new $classinstring;
$classinstring::getType();
OR
Maybe your file just could not find and access the class x\y\z\h\Foo. Make sure that your class Foo has the namespace of namespace \x\y\z\h.

Creating a PHP object where the classname is a variable

Why do I receive an error without using a fully qualified name for Bar?
Foo.php
<?php
namespace Bla\Bla;
require '../../vendor/autoload.php';
class Foo
{
public function getBar()
{
$className="Bar";
$fullClassName='\Bla\Bla\\'.$className;
$obj1=new Bar(); //Works
$obj2=new $fullClassName(); //Works
$obj3=new $className(); //ERROR. Class Bar not found
}
}
$foo=new Foo();
$foo->getBar();
Bar.php
<?php
namespace Bla\Bla;
class Bar {}
$obj1=new Bar();
The statement will work because Bar is defined in namespace Bla\Bla that you are already in.
$obj2=new $fullClassName();
This will work because you are referring to the class from the global namespace.
$obj3=new $className();
This will not work because you try to initiate class Bar from a string, in which the current namespace Bla\Bla is not prepended to the class name.
It would work if you define a class Bar inside the global namespace.
#Bar.php
<?php
namespace Bla\Bla{
class Bar {}
}
namespace {
class Bar {
public function __construct(){ echo 'Hi from global ns!';}
}
}
You have to use the full namespace of the class. Try this: (not tested)
$className="Bla\\Bla\\Bar";
In case the namespace is Bla\Bla.

PHP: Namespace and calling one class into other

I have two classes, Class1 and Class2 which are under namespace myNameSpace.
I want to create an instance of Class2 in classand I am getting an error in implementing fileClass 'myNameSpace\Class2' not found in.. `. Code given below:
Class1.php
namespace myNameSpace {
use myNameSpace\Class2;
class Class1
{
public function myMethod()
{
$obj = new Class2();
}
}
call.php
namespace myNameSpace {
include 'Class1.php';
error_reporting(E_ALL);
ini_set('display_errors',1);
use myNameSpace\Class1;
$o = new Class1();
$o->myMethod();
}
If they're both in the same namespace you should not have to use a "use" statement. Seems more likely that you're not simply includeing both files.
Maybe what you're looking for is autoloading? http://php.net/manual/en/language.oop5.autoload.php

Get namespace of unqualified class name

Consider the following PHP code
namespace foo\bar;
class MyClass {}
namespace xyz;
use foo\bar\MyClass;
class OtherClass {}
$a = new MyClass();
$b = new OtherClass();
$a is an instance of \foo\bar\MyClass, while $b is an instance of \xyz\OtherClass. These fully qualified class names, I can get by calling get_class($a) and get_class($b). But is there a way to get the fully qualified names without instantiating the objects? Is there something like a magic function fully_qualified_class_name such that fully_qualified_class_name('MyClass') would return \foo\bar\MyClass, and fully_qualified_class_name('OtherClass') would return \xyz\OtherClass? Or do I just have to write the fully qualified name myself?
From php 5.5 you can use the class name resolution via ::class
namespace foo\bar {
class MyClass {}
}
namespace xyz {
use foo\bar\MyClass;
class OtherClass {}
echo 'OtherClass namespace: ', OtherClass::class, "\n"; // print "OtherClass namespace: xyz\OtherClass"
echo 'MyClass namespace: ', MyClass::class, "\n"; // print "MyClass namespace: foo\bar\MyClass"
}

PHP namespaced class within variable

Can anyone explain why the below code causes a "Cannot find class" error? Instantiating the class with the fully qualified name works, but eliminates the advantage of the "use" statement.
<?php
namespace
{
use Foo\Bar;
new Bar; // Works
$class = 'Foo\Bar';
new $class; // Works
$class = 'Bar';
new $class; // "Cannot find class" error
}
namespace Foo
{
class Bar {}
}
Thanks
Well, I suppose it's actually a feature. And aliases won't help here, for the same reasons:
Importing is performed at compile-time, and so does not affect dynamic
class, function or constant names. [...]
<?php
use My\Full\Classname as Another, My\Full\NSname;
$obj = new Another; // instantiates object of class My\Full\Classname
$a = 'Another';
$obj = new $a; // instantiates object of class Another
?>
And yes, it sorts of defeats the purpose of use with dynamic classes.

Categories