How to unset namespace in php to access global class - php

I have a lot of SDKs which have the same class names so I have moved them into folders and created namespaces. These sdks are generally used to access APIs.
//directory e.g. vendor/company/sdk/files
vendors/ebat/feeback/environment.php
I then have completely separate libraries which I have created myself which contain any methods I need to interact with these sdks. also namespaced.
//directory e.g.
library/marketplace/ebay/feedback.php
Not sure what the correct terminology is but within these libraries are some global objects e.g. stdClass and ReflectionClass which a cant seem to access/find.
feedback.php
namespace Marketplace\Ebay;
class Feedback extends \Ebat\Feedback\EbatNsFeedback_Environment {
//simple example
public function new_ReflectionClass($obj){
return new ReflectionClass($obj);
}
}
Now if I were to try and call this method
$this->marketplace->ebay->feedback->new_ReflectionClass(new stdClass()));
this error is shown.
Fatal error: Class 'Marketplace\Ebay\ReflectionClass' not found
When I add a backslash
return new \ReflectionClass($obj);
it then gives me this error
Fatal error: Class 'Ebat\Feedback\ReflectionObject' not fount
How can I access the native/global ReflectionClass class, all similar questions seem to be fixed by adding a backslash.

Adding a leading \ is the correct way to access top-level namespaces/classes.
But it seems you're running in to a similar error elsewhere - note that your 2nd fatal error is about ReflectionObject, not ReflectionClass. Do you have other code that tries to access ReflectionObject?
Also, note that you can also import these namespace-less classes if you don't want to prefix every reference with a \
namespace Marketplace\Ebay;
use \ReflectionClass;
class Feedback extends \Ebat\Feedback\EbatNsFeedback_Environment {
//simple example
public function new_ReflectionClass($obj){
return new ReflectionClass($obj);
}
}

Related

Using namespaces in PHP

I am working on the AWS documentation which uses Guzzle framework. I have to deal with namespaces here and I am not able to get it working. I went through the docs and examples and understood that we can have packages for projects using namespaces.
I went ahead and tried a simple example, but unsuccessful. Here's the example: this is the index.php:
use My\Full\Classname as Another; //Also tried use My\Full\Classname
$obj = new Another; //with $obj = new Classname;
echo $obj->add();
I have Classname.php in the directory structure like this My->Full->Classname.php:
<?php
class Classname{
public static function add(){
return 2+2;
}
}
?>
I am trying to call the function in index.php but getting error:
Fatal error: Class 'Another' not found in C:\wamp\www\guzzleEx\index.php on line 19
which is the line where I instantiate the Classname object $obj = new Another;
What is the mistake i am making? Is there any INI that needs to be updated or any other config issue? How can I make the code working? If you use the normal include for Classname.php it works fine.
Namespaces need to be explicitly declared, they do not come from a certain directory structure.
So if you do not have a line that reads namespace My\Full; in front of your class Classname, then your class is not in any namespace, but in the root namespace.
Thus you cannot use it as \My\Full\Classname, but \Classname or even Classname directly.

PHP Namespaces & Referencing Classes not contained within Namespaces

I have a simple question, which should hopefully have a quick answer. The code I have written makes heavy use of namespaces (I use fully qualified names everywhere); however, a piece of code (a calendar / date picker control, not written by myself) needs to be included. When I attempt to create the control, it assumes the current namespace (GUI), resulting in this error: "PHP Fatal error: Class 'GUI\tc_calendar' not found in C:\inetpub\wwwroot\Calico\calico_classes_v2.php on line 1852". Now, the calendar control itself (and it's underlying class file) does not make use of namespaces, and I am a little worried about attempting to modify it (an earlier attempt did not go well).
How can I import / include a control, that is not contained within a namespace, into the rest of my code, that does? Does PHP have something like "Import class calendar from file AS \Calendar\Calendar"?
Edit:
For additional information: I have a class, called "tc_calendar", contained in a file called "tc_calendar.php". It is not part of any namespace.
In a separate file, I have several classes (Bitmap, CompositeCalendar, EventEditor, TimeExtractor), all contained within their appropriate namespaces (GUI, Data, Extract, etc.).
In one of those classes, CompositeCalendar, contained within the GUI namespace, I am trying to create an instance of a tc_calendar. However, PHP is throwing an error (above). tc_calendar is not a part of any namspace (and definitely not a part of the GUI namespace, which PHP is just assuming, because it can't seem to find it), and I need help creating an instance of it.
tldr; $newcontrol = new tc_calendar() doesn't work; PHP tries to guess the namespace for it (because one isn't specified, because tc_calendar isn't a part of any namespace), comes up with \GUI\tc_calendar (obviously wrong). How do I access a class, not contained within a namespace, from inside a namespace?
Do you mean something like this:
namespace GUI;
use \tc_calendar as Calendar;
$myCalendar = new Calendar();
The as Calendar is optional. You could aswell keep it with the original name tc_calendar if you ommit the as Calendar.
Update
To put it in shape of the comment:
namespace {
class tc_calendar {}
}
namespace GUI {
use \tc_calendar;
class CompositeCalendar {
private function blah() {
$control = new tc_calendar();
$control->stuff();
}
}
}
I wouldn't copy paste external libraries into he same file though. It bad practise. It is better to keep them in another file and then include them and have the following:
namespace GUI;
use \tc_calendar;
require_once 'tc_calendar.php';
class CompositeCalendar {
private function blah() {
$control = new tc_calendar();
$control->stuff();
}
}
Or combine my 3 snippets to have it any other form you like.
Also I would suggest to extend the calendar if you are just building calendar class based on the the tc_calendar:
namespace GUI;
use \tc_calendar;
require_once 'tc_calendar.php';
class CompositeCalendar extends tc_calendar {
private function blah() {
$this->stuff();
}
}
Any class not in a namespace is automatically in the global namespace.
To refer to anything in the global namespace from anywhere, use a single preceding \:
new \tc_calendar;

Instantiating a class within a recursive function in PHP

I've been creating a script using PHP and seem to have hit a brick wall at the moment with it. I'm trying to call a variable from another file which is within a class, so I went about it by including the file within the original and then instantiating the class to call the variable. But the entire thing is within a recursive function and I get Fatal error: Cannot redeclare class JVersion. I've pasted part of the code below:
...
function functionname($path)
{
...
define('_JEXEC', true);
require_once ($path ."libraries/cms/version/version.php");
$test_class = new JVersion();
$jma_ver = $test_class->getShortVersion();
...
functionname($path);
...
}
This is how the part of the code within the version.php looks like
defined('_JEXEC') or die;
final class JVersion
{
public $RELEASE = '2.5';
public $DEV_LEVEL = '1';
public function getShortVersion()
{
return $this->RELEASE.'.'.$this->DEV_LEVEL;
}
Is there a way I can overcome the "Fatal error: Cannot redeclare class JVersion" error ?
If I understand the problem correctly (i.e. you have many classes named JVersion that you need to use), then I'm not sure of any way around this problem without namespacing your JVersion classes in some way. Either give them distinct names or put them in different PHP namespaces.
This sounds more like an architectural problem, really; the code you're using doesn't seem to provide a way of getting its version in a disposable manner (without setting yourself up for conflicts like this). Ideally, you should just have a global JVersion class that can be insantiated with the appropriate version numbers; this way there'd be no class name conflicts.

Is it bad to include all the Namespaces and access Classes with just their class name in PHP?

Recently I have started to play around with Namespaces in PHP, I am not a fan of the chosen Namespace separator / (as most others have a mutual feeling) /
Below I have a very basic/simple example of using Namespace in PHP.
I have a Registry.class.php file that has a Registry class that is in the Namespace Library
I also have the file Controller.class.php below that has a Controller class that is in the Namespace called Project
Now in this Project Namespace I need access to the Registry object that is in a different Namespace, Library So I use use Library\Registry to Import that Namespace
With that done, I am able to simply access my Registry class like this...
$this->registry = new Registry;
Now if I had not used use Library\Registry then I would have to access it like this
$this->registry = new /Library/Registry;
So this is where my question begins.
For this simple example I am just bringing in the Registry class, in a real project, imagine several other classes and namespaces being brought in, So I would be using
use Library\Registry and use Library\SOME OTHER CLASS and use Library\YET ANOTHER CLASS etc...
I can then operate on the Objects/classes as if they were in this namespace to begin with so instead of pre-fixing the class name with its appropriate Namespace name, I simply just instantiate the class by its name ( new Registry;) instead of new /Library/Registry;
I would like to know if this is considered bad practice or not? It greatly simplify's things and makes the code look cleaner IMO, but if there is anything bad about this method I would like to know before I write a lot of code using this method. Should I be pre-fixing my classes when I instantiate them with their namespace INSTEAD of importing them with the use /Path/to/Namespace/Classname ?
Please share your knowledge with me, I realize it will work either way but I want to make sure. Example classes and namespaces are added below:
Controller.class.php
<?php
namespace Project
{
use Library\Registry;
class Controller
{
public $registry;
function __construct()
{
include('E:\Library\Registry.class.php');
$this->registry = new Registry;
}
function show()
{
$this->registry;
echo '<br>Registry was ran inside testcontroller.php<br>';
}
}
}
?>
Registry.class.php
<?php
// Registry.class.php
namespace Library
{
class Registry
{
function __construct()
{
echo 'Registry.class.php Constructor was ran';
}
}
}
?>
One of the primary purposes of namespaces is to prevent name conflicts in large code bases and across shared code and imported libraries. If you're not having name conflict issues, then I don't necessarily see anything wrong with using each class. If you find yourself doing this for all your classes, however, then I would question why you're using namespaces in the first place. This would only seem to make sense to me if your code was going to be used as an API or library by someone else.

PHPUnit: stub methods undefined

I must be missing something. I'm trying to stub methods on a class in PHPUnit, but when I invoke the method on the mock object, it tells me that method is undefined.
Example class to stub:
namespace MyApp;
class MyStubClass
{
public function mrMethod()
{
// doing stuff
}
}
To stub it, I write:
// specifying all getMock() args to disable calling of class __construct()
$stub = $this->getMock('MyStubClass', array(), array(), 'MockMyStubClass', false, false, false);
$stub->expects($this->any())
->method('mrMethod')
->will($this->returnValue('doing stuff'));
But upon invoking the stubbed method, I get an exception:
$stub->mrMethod();
//PHP Fatal error: Call to undefined method MockMyStubClass::mrMethod()
I'm using PHPUnit 3.4.3 with PHP 5.3.0.
Another small thing I noticed was that if specifying a namespace in the getMock() method results in a class loading exception because of a double namespace:
$stub = $this->getMock('MyApp\MyStubClass');
// Fatal error: Class 'MyApp\MyApp\MyStubClass' not found
That strikes me as rather odd (and getmock() will not accept a namespace with a leading backslash). The only thing I could think to cause that would may be because this class is
registered with an autoloader?
Any thoughts?
Answering my own question:
After quite a bit of frustration, I did manage to get things working. I'm not sure precisely what the issue was, but did discover a few things that might help others:
Make sure you're running the latest version of PHPUnit (3.4.6 as of this writing)
Use the fully-qualified namespace minus the first backslash.
$this->getMock('MyApp\Widgets\WidgetFactory');
Part of my problem was that PHPUnit was creating a stub class WidgetFactory that was not actually stubbing MyApp\Widgets\WidgetFactory. One would expect that an exception would occur if trying to stub a non-existent class, but it doesn't seem to happen with the namespace confusion.
Also, there is a question over here that suggests using the class alias method as follows:
class_alias('MyApp\Widgets\WidgetFactory', 'WidgetFactory');
$this->getMock('WidgetFactory');
While this did temporarily solve my problem, I would strongly advise against using it. class_alias() cannot be called twice for the same alias without raising an exception, which causes obvious problem if used in the setup() method, or as part of the stub generation.
I had a similar issue, my problem was that the path I was writing was something like MyApp\Widgets\WidgetFactory\MyStubClass while the class was something like this:
namespace MyApp;
class MyStubclass
{
public function mrMethod()
{
// doing stuff
}
}
So there wasn't the uppercase C in the name of the class in the path

Categories