I am trying to get php's namespace technique down but having some issues here:
Why can I not call the method testA from class B? Also I would like to add that I am using 5.3.
in lib ->
<?php
namespace lib;
class A{
public static function testA(){
echo "I am inside A";
}
}
in root dir ->
<?php
class B{
public function showSomething(){
lib\A::testA();
}
$test = new B();
$test->showSomething();
Using namespace you only encapsulate item but not include it. I can't see including of the class file. Include it or use autoload.
Make sure you're including the file that holds your class A.
Related
I created a class at Controller folder of Cake project like this:
<?php
class Hi
{
function __construct(){ }
public function hi()
{
echo "hi!";
exit;
}
}
Then in a controller, I tried to include it:
<?php
namespace App\Controller;
use App\Controller\AppController;
include_once "Hi.php";
class MyController extends AppController
{
public function sayHi()
{
$a = new Hi();
$a.hi();
}
}
Here is the error I'm having:
Fatal error: Cannot declare class Hi, because the name is already in use in path\api\src\Controller\Hi.php on line 2
What's going on?
MyController.php and Hi.php are in the same folder. I'm using PHP 7.
Including a file won't make the classes in that file part of the current namespace, as namespaces are a per-file functionality.
http://php.net/...namespaces.importing.php#language.namespaces.importing.scope
Your Hi class will be declared in the global namespace, and your new Hi() will cause PHP to look for it in the current namespace, ie it will look for App\Controller\Hi, which doesn't exist, hence the composer autoloader kicks in, and will map this via a PSR-4 namespace prefix match to src/Controller/Hi.php, which will include the file again, and that's when it happens.
http://www.php-fig.org/psr/psr-4/
Long story short, while using new \Hi() would fix this, you better not include class files manually, or declare them in paths where they do not belong. Instead declare your files and classes in a proper autoloading compatible fashion, that is for example with a proper namespace in a path that matches that namespace, like
namespace App\Utils;
class Hi {
// ...
}
in
src/Utils/Hi.php
I need to define a class in many classes and i should use all of this class in one class ! And $this dosn't work on this way .
Undefined variable
class main extends Controller{
//some codes
}
In another file
class site{
function test(){
include main.php;
$obj = new main();
class first{
function name(){
$obj->somefunction();
}
}
class second{
function family(){
$obj->somefunction();
}
}
}
}
First of all, class declaration cannot be nested: You can't declare a class within a class, nor within a function.
As per your second question, if you want to have different classes with the same name, you should use namespacing to differentiate them.
Also, you should follow the PSR coding style convention (http://www.php-fig.org/psr/psr-2/)
Have a look at : http://php.net/manual/en/language.namespaces.php
EDIT:
Yes the problem was using \ at the beginning of the use statement. As M1ke pointed out, use goes from the root element.
Original post
I think is a PHP question but it may be Drupal.
I'm working on a headless Drupal project where is using a class (which I call Entity Model) that uses a Drupal class called EntityFieldQuery.
Before a create or use this class I bootstrap Drupal using:
require_once DRUPAL_ROOT.'/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
The entity model class is in the Models names space like so:
namespace Models;
use \EntityFieldQuery;
class EntityModel
{
.....
$query = new EntityFieldQuery();
$query->doSomething();
......
}
The EntityFieldQuery is found perfectly as I use the "\" because this class is out of the Models namespace.
The problem is when this class is created is uses other classes that don't use any namespace, and I have the following error:
class Models\InsertQuery not found in ....
Here is the class used by EntityFieldQuery that uses InsertQuery
class InsertQuery_mysql extends InsertQuery ...
I don't understand why InsertQuery_mysql is found but InsertQuery
I ended up adding a "\" in InsertQuery to fix the problem like so:
class InsertQuery_mysql extends \InsertQuery ...
Actually this class in a php file called query.inc that contains two defitinion classes (in the same file, I don't know this is a a problem too)
class InsertQuery_mysql extends InsertQuery
....
class TruncateQuery_mysql extends TruncateQuery
I thought that if I use "new \ClassName()" the "default namespace" inside this class would be "\" too and not the first called class's namespace.
I don't like to modify 3rd party libraries, is any way to avoid this? I guess is a architecture problem rather than a lack of definition if someone has a better idea, I appreciate.
Thanks!
EDIT2: Adding more info...
In order of execution.
index.php:
require_once 'vendor/autoload.php';
require_once DRUPAL_ROOT.'/includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
...
app/SiteController.php:
use Models\Campaign;
class SiteController {
...
$campaing = new Campaign();
...
app/Models/Campaing.php:
namespace Models;
class Campaign extends EntityModel {
...
app/Models/EntityModel.php:
namespace Models;
use \EntityFieldQuery; //<-- this should go without \ as I say in EDIT section
class EntityModel {
...
public function getAll() {
$query = new EntityFieldQuery(); //<--throwed Models\InsertQuery not found. It must have \ at the beginning of the class name.
To answer the base question (and pending further code) PHP namespaces are set by whichever namespace is declared in the file.
// Bar.php
namespace Foo;
class Bar {}
// some other file
use Foo\Bar;
$test = new Bar(); //works
// different file
namespace Foo;
$test = new Bar(); // works
// another file
require 'Bar.php';
// won't work because we are not in namespace "Foo"
$test = new Bar();
In your specific case it the use \EntityLoader should be use EntityLoader because you're exiting the namespace you want to be inside.
I have been looking into name spaces recently. I am creating an MVC framework and want to try to move towards PHP 5.3+ features.
Lets say I define a namespace, call it \Controller.
I then want to include a file which has the class of Home.
namespace Controller;
include "class.home.php";
The file contents:
class.home.php:
class Home {
public function hello() {
}
}
In this example, will Home be a part of the Controller namespace? or part of the global namespace?
I want to be able to access Home like new \Controller\Home(); Will the code above work like that?
The namespace of a class depends on the namespace it was declared in, not in which it was included. If the latter was the case, it'd be impossible to clearly know what namespace something will be in.
If you did not write namespace Foo\Bar\Baz; at the top of the file in which you declare the class, the class is in the global namespace, always.
In your case Home will not be part of the Controller namespace, because you need to define the namespace inside of class.home.php like:
namespace Controller;
class Home {
public function hello() {
}
}
And than you can:
namespace Controller;
include "class.home.php";
new \Controller\Home();
I have a question about namespaces in PHP.
this code doesn't work :
<?php
namespace My\Functions\Printing;
class A {
public function __construct() {
echo __NAMESPACE__;
}
}
namespace My;
use My\Functions\Printing\A as A;
$obj=new namespace\A();
But this one work :
<?php
namespace My\Functions\Printing;
class A {
public function __construct() {
echo __NAMESPACE__;
}
}
namespace My;
use My\Functions\Printing\A as A;
$obj=new A();
I would like to get more information about the behavior of namespaces importation.
Why an imported class can't be accessible in the namespace where it is imported?
Probably you have a confused idea of using 'use'.
The keyword 'namespace' refers to the current namespace
namespace My\Functions\Printing;
class A {
public function __construct() {
echo __NAMESPACE__;
}
}
namespace My;
use My\Functions\Printing\A;
use My\Functions\Printing\A as myAlias;
$obj=new namespace\A(); // instance of \My\A (doesn't exist)
$obj2=new A(); // instance of \My\Functions\Printing\A
$obj3=new myAlias(); // instance of \My\Functions\Printing\A
As usual, see the documentation for complete details: php doc
I'm not sure but I think problem is in using namespace keyword.
http://www.php.net/manual/pl/language.namespaces.nsconstants.php
In first example you are in My namespace so namespace\A() == My\My\Functions\Printing\A()
My\Functions\Printing namespace look like
-- My\Functions\Printing
A
"My" namespace before importation look like:
-- My
"My" namespace after importation should look like (i thinks)
-- My
A
So why i can't acces My\A ?
I think the structure of namespace it's not changed an PHP compiler check in the "namespace imported area" before the "namespace structure".
Documentation specify namespaces look like filesystem, it's false.
If an importation of an other class is as "create a symbolic link" i must be able to acced it from the current namespace.
We can acced a symbolic link from the directory where it was created or from the absolute path but it's not possible to acced the imported class
with its new path. (\My\A)
Why ?