I have the following PHP code:
<?php
namespace my_ns;
class dummy
{
function do_print(){echo "printing...";}
}
$obj_dummy=new dummy();
$obj_dummy->do_print();
?>
This works all fine,
How ever If I put the class in a separate include file (e.g. class.dummy.php), making the code on the page look like below:
<?php
namespace my_ns;
include ("class.dummy.php");
$obj_dummy=new dummy();
$obj_dummy->do_print();
?>
I get the error message:
Class 'my_ns\dummy' not found in ...
How can by default make sure that (all) include-files are automatically added to a given namespace?
A file containing a namespace must declare the namespace at the top of the file before any other code - with one exception: the declare keyword.
Source
Add this to your included file as well.
namespace my_ns;
After that, your code works just fine.
Reference
Related
I'm learning about PHP namespaces. I'm attempting to add a namespace to a class I wrote, and then reference it in another script. I've done it like this:
MenuBuilder.php
namespace Andytest\MenuBuilder;
class MenuBuilder {
public function test() {
echo 'testing';
}
}
test.php (where the class is used)
namespace Andytest\MenuBuilder;
require_once 'MenuBuilder.php';
$Builder = new MenuBuilder;
$Builder->test();
The output from this is as I expect - it outputs the word 'testing' when I run test.php.
But I'm not sure why I need namespace Andytest\MenuBuilder in test.php because it's already decleared in MenuBuilder.php, which is being required by test.php? If I remove the namespace line in test.php it doesn't work.
Am I doing this correctly?
In test.php you should have use Andytest\MenuBuilder\MenuBuilder; instead of namespace Andytest\MenuBuilder;
Because you use namespace instead of use the file expects every class you create that is not specified in the use case to be in the same namespace, therefore new MenuBuilder(); actually calls new Andytest\MenuBuilder\MenuBuilder();.
If you would change the namespace in test.php it should tell you that it cant find MenuBuilder
I've created a class in symfony, the file path is
src/Project/MemberBundle/Document/EnumMemberType.php
and the file looks like this
<?php
namespace Project\MemberBundle\Document;
class EnumMemberType {
const VIP = 'xxxxx';
} // class - EnumMemberType
but when i try to get the constant in this class in another file using the code
$memberType = \Project\MemberBundle\Document\EnumMemberType::VIP
i got the error
Attempted to load class "EnumMemberType" from namespace "Project\MemberBundle\Document".
Did you forget a "use" statement for another namespace?
I really can't figure out why, the file name is the same with the class name, and it's under the right folder, and the file starts with <?php.
Can anyone see what else i'm missing? Thanks
Maybe problem with namespace. You can write in the header of another file:
use Project\MemberBundle\Document\EnumMemberType
and
$memberType = EnumMemberType::VIP;
i am using a simple php code with Activexpert to send sms here the intersting part
<?php
if(isset($_POST["submit1"]))
{
$_objSmsProtocolGsm = new Com("ActiveXperts.SmsProtocolGsm");
...
}
?>
it working fine but when i tried to insert it into my controller in symfony
i get an error
Attempted to load class "Com" from namespace
"PFE\SiivtBundle\Controller" in C:\Program Files
(x86)\EasyPHP-DevServer-14.1VC11\data\localweb\Symfony2.5\src\PFE\SiivtBundle\Controller\SiivtController.php
line 835. Do you need to "use" it from another namespace?
while my php code is simple and doesnt include or require any other files
Error message is self-explanatory.
Com class doesn't exist in PFE\SiivtBundle\Controller namespace (which is current namespace in your controller class).
Try:
new \Com("ActiveXperts.SmsProtocolGsm");
The backslash before classname means that we're looking for a class in global namespace
I am developing with php 5.5, I provided code of my file here
<?php
require_once 'jsonrpcphp/includes/jsonRPCClient.php';
class Client
{
private $remoteMain;
public function __construct($param)
{
$this->remoteMain = new jsonRPCClient('http://
urlTofile/nameOfFile.php');
This codes works absolutely fine but the issue comes when I need to put a namespace for the file as soon as I put a namespace at the top of the file, for example:
<?php
namespace packagename\subPackage;
require_once 'jsonrpcphp/includes/jsonRPCClient.php';
class Client
This error will be displayed
Class 'packagename\subPackage\jsonRPCClient' not found in
The question is this:
how to access a class which is not in my namespace and provided from 3rd parties when I need to have namespaces
thanks in advance
If you're in namespace packagename\subPackage, then new jsonRPCClient refers to the class packagename\subPackage\jsonRPCClient. If you want to use a class from the global namespace, you need to explicitly specify that:
new \jsonRPCClient
Alternatively, explicitly alias the class at the top of the file:
use jsonRPCClient;
new jsonRPCClient(...)
Please RTM: http://php.net/manual/en/language.namespaces.basics.php
actually my question is very simple. In PHP I can define namespaces in files which will be loaded with require or include.
Example:
content of index.php
namespace test;
require('example.php');
die(__NAMESPACE__);
content of example.php
namespace example;
echo "hello world";
If you execute this code, the output will be "test", because the namespace will not change if you include a file with another namespace.
But could you tell me why? How can I imagine the parser?
It could look like:
namespace test{}
namespace example{}
// and here it goes on with test, but why I didnĀ“t said namespace test
namespace test{}
In a PHP File, all code after a namespace declaration will belong to that namespace. This includes the require statements.
Each PHP file has within it its own value for __NAMESPACE__ that is populated when namespace is declared. To access the namespace of another file, you could create a method in example.php that returns __NAMESPACE__. See this question.
Alternately, you could use get_current_namespaces() but this won't tell you what namespace belongs to what file.