How to resolve fatal error when using namespace in a class - php

I have a PHP class that I would like to add a namespace to it.
I am using PHP Version 5.5.12 on Windows Server 2008 R2 and Apache 2.4
My Permissions class in located in /classes/Permissions.php
class Permissions
{
private $db;
public function __construct(){
}
public function sayHello(){
echo 'Hello';
}
private function _test(){
}
}
?>
When I want to start a new instance of the class, I do this in a file located "/test.php"
require_once 'classes/Permissions.php';
$r = new Permissions();
$r->sayHello();
And this is working fine.
Now I am trying to add namespace "which I never used before"
I changed my code to implement namespace like so
<?php
namespace classes\Permissions;
class Permissions
{
private $db;
public function __construct(){
}
public function sayHello(){
echo 'Hello';
}
private function _test(){
}
}
?>
and when staring a new instance of the class I do this
require_once 'classes/Permissions.php';
$r = new classes\Permissions();
$r->sayHello();
But with this I get a fatal error
Fatal error: Class 'Permissions' not found
What am I doing wrong? and how to correct it?

If you're going to require the file manually with require_once you're free to put the class under any namespace you like. So you can go:
namespace Foo;
class Permissions {}
You can instantiate this class with:
require_once 'path.to.classfile.php';
$r = Foo\Permissions(); //if you're in global namespace
docs: http://php.net/manual/en/language.namespaces.php
So when you have a class like this:
namespace classes\Permissions;
class Permissions {}
The way to create an object after you include it is with new classes\Permissions\Permissions(); again assuming you're in global namespace.

You should call classes with backslash at begin of namespace.
Try like this:
$r = new \classes\Permissions();

Is not it \classes\Permissions\Permissions()? You have a class Permissions in \classes\Permissions namespace

Related

Can't extend abstract PHP class

I want a base class to be extended, but I have some errors coming out:
Fatal error: Class 'Api\Services\Base' not found in
/var/www/html/Api/Services/Example.php on line 7
I searched for typos, tried to use the fully qualified name, made the abstract class empty or just defined it as a simple class; none of these helped.
Using "require" instead of "use" worked, but still...
Any idea (the two files are in the same directory: /var/www/html/Api/Services)?
Thanks in advance...
<?php
// Base.php
namespace Api\Services;
use Api\Classes\ErrorHandler;
use Api\Classes\ErrorMessage;
abstract class Base
{
public $data = null;
public function getData()
{
return $this->data;
}
public function setData($data = null)
{
$this->data = $data;
}
}
?>
<?php
// Example.php
namespace Api\Services;
use Api\Services\Base;
class Example extends Base
{
public $request = array();
public function __construct($request = array())
{
$this->request = $request;
}
}
?>
use Base
instead of
use Api\Services\Base;
because you are already inside the namespace Api\Services
Actually, you don't even have to write the use statement, you are inside the namespace, you can just call the classes inside the same namespace without including them (use)

use class from namespace in php in class method

I'm new to php and trying to use namespaces for the first time and I have this crazy problem in a big php file (simplified below):
B.php:
namespace Logic;
class C {}
class B {}
A.php:
use Logic\C;
class A extends \BaseClass {
public function __construct() {}
// [...500 lines of code...]
private function hi() { $c = new C(); }
}
The hi method gives the error: Class 'Logic\\C' not found in A.php
But if I just reference B in the constructor of A, it works as expected:
class A extends \Base {
public function __construct() { $dummy = new C(); }
// [...500 lines of code...]
private function hi() { $c = new C(); }
}
When the hi method in the modified code above is run, there are no problems.
Can anybody think of a reasonable explanation for why this happens? Am I misusing namespaces in php?
You need to understand, that the use statement doesn't automatically include the source code file where Logic\C is defined. You need to use an autoloader, or manually require_once that file before accessing classes from that file.
I suggest to follow the manual about namespaces (and the examples there): http://php.net/manual/en/language.namespaces.php

Call function from different class and namespace

I'm having some trouble to call a function from a namespaced class in a different namespaced class. In the dummy example below I would like to know how to use Class2 within Class1. I'm getting the error:
Trait 'name1\name2\Class2' not found in class1.php
The code:
#file index.php
require "class1.php";
require "class2.php";
$class1 = new name1\Class1();
$class1->sayHello();
#file class1.php
namespace name1{
class Class1{
use name2\Class2;
public function sayHello(){
echo Class2::staticFunction();
}
}
}
#file class2.php
namespace name2{
class Class2{
public static function staticFunction(){
return "hello!";
}
}
}
Thank you for any advice.
Ok, so you've got several errors which I have fixed.
Here's the working code you need:
# index.php
include "class1.php";
include "class2.php";
$class1 = new name1\Class1();
$class1->sayHello();
# class1.php
namespace name1;
use name2\Class2;
class Class1{
public function sayHello(){
echo Class2::staticFunction();
}
}
# class2.php
namespace name2;
class Class2{
public static function staticFunction(){
return "hello!";
}
}
Some explanations:
When in class definition the use is used for using traits and not namespace
In PHP namespace need not be enclosed in curly brackets
In PHP you include files with include, include_once, require, or require_once, and not import
Inside your first class, your trait is calling class2 as use name2\Class2 but, you are still within the name1{} namespace, so in reality you are calling it as: use name1\name2\Class2
So, you need to change
use name2\Class2; to use \name2\Class2
Try this.
namespace name1{
use \name2\Class2;
class Class1{
public function sayHello(){
echo Class2::staticFunction();
}
}
}
#file class2.php
namespace name2{
class Class2{
public static staticFunction(){
return "hello!";
}
}
}
Also, another tip: If you are separating your classes in separate files, you do not need to separate them as in they way you have done. Just call the namespace simple as:
// file1.php
namespace person;
class name{}
//file2.php
namespace address;
class name{}
Why not drop the static method and just inject the class? Seems like going through extra work for something so simple. That's what function arguments are made for.
namespace name1{
use \name2\Class2;
class Class1{
public function sayHello($Class2){
echo $Class2->someFunction();
}
}
}
namespace name2{
class Class2{
public function someFunction(){
return "hello!";
}
}
}
#index.php
include "class1.php";
include "class2.php";
$Class1 = new name1\Class1();
$Class2 = new name2\Class2();
$Class1->sayHello($Class2);
//hello!

Instantiate a class in another class code and get rid of include("class_name"), using namespace

so i have a class here that have a function who requires another class to create an object.
I use namespace in both files, my question is can i get rid of this line here: include("class.php"); and instantiate class using namespace?
here is the file from where i call the other class:
namespace namespaceName;
class classLoader{
public function __construct() {
//not used
}
public function executeFunctionOutsideTheNamespace() {
include("class.php");
new classExtended("badass");
}
}
and the class by itself:
namespace namespaceName;
class classExtended extends classBase
{
public function __construct($action) {
echo $action;
}
}
I ask again, using Namespace there is no possibility to get rid of include() or require(), require_once() functions? to call directly new classExtended("badass"); ?

Cannot use a Namespaced Class inside another namespace in PHP

I am still having trouble with PHP5 Namespaces.
I have a namespace called Project and I am trying to access a class called registry inside of this Project namespace that has a namespace of Library so at the top of the file that is a Project namespace I use this line use Library\Registry;
Registry class is inside the Library Namespace
This should work but it doesn't, instead the ONLY way to access my registry class inside this Project namespace is to use this
$this->registry = new \Library\Registry;
I want to be able to use this instead...
$this->registry = new Registry;
That was the whole reason of using
use Library\Registry;
at the top of the Project namespace file
Below I have 3 small example scripts in a folder structure like this.
Library/registry.class.php a class in my Library folder
Controller/controller.class.php and class in my Controller directory
Controller/testing.php a test file to run the script.
E:\Library\Registry.class.php file
<?php
namespace Library
{
class Registry
{
function __construct()
{
echo 'Registry.class.php Constructor was ran';
}
}
}
?>
E:\Controller\Controller.class.php file
<?php
use Library\Registry;
namespace Project
{
class Controller
{
public $registry;
function __construct()
{
include('E:\Library\Registry.class.php');
// This is where my trouble is
// to make it work currently I have to use
// $this->registry = new /Library/Registry;
// But I want to be able to use it like below and that is why
// I have the `use Library\Registry;` at the top
$this->registry = new Registry;
}
function show()
{
$this->registry;
echo '<br>Registry was ran inside testcontroller.php<br>';
}
}
}
?>
E:\Controller\testing.php file
<?php
use Project\Controller;
include('testcontroller.php');
$controller = new Controller();
$controller->show();
?>
I get this error...
Fatal error: Class 'Project\Registry' not found in PATH to file
unless I use this below on the controller.class.php file
$this->registry = new \MyLibrary\Registry;
Because in that file at the top I have use Library\Registry; I should be able to access it like this...
$this->registry = new Registry;
Please help me get it where I can use it like that instead
use Library\Registry;
namespace Project
{
I believe that's the wrong way round: you're useing Library\Registry in the global namespace, and then opening the Project namespace.
Put the use statement inside the namespace you want to import it into.
namespace Project
{
use Library\Registry;
You need to import your Registry class inside Project namespace, because you need em there, not in global scope.
<?php
namespace Project
{
use Library\Registry;
class Controller
{
public $registry;
function __construct()
{
include('E:\Library\Registry.class.php');
// This is where my trouble is
// to make it work currently I have to use
// $this->registry = new /Library/Registry;
// But I want to be able to use it like below and that is why
// I have the `use Library\Registry;` at the top
$this->registry = new Registry;
}
function show()
{
$this->registry;
echo '<br>Registry was ran inside testcontroller.php<br>';
}
}
}
?>
Just add:
use \Library\Registry;
at top of your script under the namespace declaration
Then you can just say:
$registry = new Registry;
inside your class
By the way your class declaration is all wrong. You should not wrap your namespace inside curly braces, namespace is not a function.
This is how it should be. Also make sure the class declaration of Library\Registry is already included by either using include('/path/to/registry.php'); or using autoloader
namespace Project;
include('E:\Library\Registry.class.php');
use \Library\Registry;
class Controller
{
public $registry;
function __construct()
{
// This is where my trouble is
// to make it work currently I have to use
// $this->registry = new /Library/Registry;
// But I want to be able to use it like below and that is why
// I have the `use Library\Registry;` at the top
$this->registry = new Registry;
}
function show()
{
$this->registry;
echo '<br>Registry was ran inside testcontroller.php<br>';
}
}
Enjoy
<?php namespace Project;
require_once 'your registry class file';
use \Library\Registry as Registry;
Now you will be able to use...
$this->registry = new Registry;

Categories