Extending un-namespaced class from a namespace - php

I have the following legacy file (can't edit):
class Test {
public $abc=1;
}
I need to extend this class from a name-spaced file:
use mynamespace;
class MyClass extends Test {
}
However my auto-load function attempts to include mynamespace\Test. How to specify that un-namespaced version of Test should be used?

Prefix the class name with a \:
class MyClass extends \Test {
...
}
From the manual on namespaces: "Note that to access any global class, function or constant, a fully qualified name can be used, such as \strlen() or \Exception or \INI_ALL."

Try this:
class MyClass extends \Test {
}

Related

Can't create a abstract class mock in another file

I have an abstract class defined in my project that I want to run tests on.
<?php
use PHPUnit\Framework\TestCase;
use phpDM\Connections\Adapters\ConnectionAdapterInterface;
include(__DIR__ . '/../../../src/Connections/Adapters/ConnectionAdapterInterface.php');
class TestConnection { }
class ConnectionAdapterInterfaceTest extends TestCase
{
function testCreateConnection() {
$interface = $this->getMockForAbstractClass('ConnectionAdapterInterface');
$interface->method('createConnection')
->willReturn(new TestConnection());
$connection = $interface::createConnection();
$this->assertInstanceOf(TestConnection, $connection);
}
}
The ConnectionAdapterInterface is a (poorly named) abstract class:
abstract class ConnectionAdapterInterface {
abstract public static function createConnection(array $config);
}
However, no matter what I do, I keep getting the error Class "ConnectionAdapterInterface" does not exist.. In the sample above, I have both a use and include, though I also tested with each one at a time.

Overwrite namespace usage in extended class in PHP

Overwrite namespace usage in extended class
Is it possible to overwrite the used namespace of the parent class without rewriting the function in the extended class?
For clarification i write down an example:
i have two classes like this:
namespace one;
class hey
{
public static function say()
{
echo "hey";
}
}
and
namespace two;
class hey
{
public static function say()
{
echo "ho";
}
}
Now i use on of the namespaces in this class:
use one\hey;
class saysomething
{
public static function main()
{
hey::say();
}
}
Now i want to extend the last class:
class extended extends saysomething
{
}
extended::main();
In this class i want to use namespace "two\one" without overwriting the function, is it possible? *f it is, how?
Thank you for your time.
You must define extended::main() within extended class, like this:
use two\hey;
class extended extends saysomething
{
public static function main()
{
hey::say(); // will call two/hey::say method
}
}
Namespaces won't help here.

Phpunit can't find abstract class

For some reason when I try to test abstract class I get PHPUnit_Framework_MockObject_RuntimeException: Class "AbstractClass" does not exist.
Here's the code
AbstractClass.php
<?php
namespace SD\Project;
abstract class AbstractClass
{
public function handle()
{
}
}
AbstractClassTest.php
<?php
require_once 'AbstractClass.php';
use SD\Project\AbstractClass;
class AbstractClassTest extends PHPUnit_Framework_TestCase
{
public function testHandle()
{
$stub = $this->getMockForAbstractClass('AbstractClass');
}
}
When I get rid off the namespace and use statements the code is executed successfully. What I'm doing wrong?
You are not using the fully qualified path of the class.
$stub = $this->getMockForAbstractClass('\SD\Project\AbstractClass');
Read Similar: PHPUnit, Interfaces and Namespaces (Symfony2)
Examples: http://theaveragedev.com/testing-abstract-classes-with-phpunit/

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!

Export class from namespace

I namespaced a class to make use of aliases to provide shorthands for some long class names:
namespace some_namespace;
use \VeryLongClassnameWhichIUseOften as Short;
class MyClass {
public static method do_stuff() {
Short::do_something(Short::do_other_stuff());
}
}
Now there is some third party code that expects MyClass in the global namespace.
Can I somehow export MyClass to the global namespace?
I tried
class \MyClass {
...
}
but apparently that is no allowed (unexpected T_NS_SEPARATOR, expecting T_STRING).
I also tried this:
namespace some_namespace {
use \VeryLongClassnameWhichIUseOften as Short;
class MyClass {
...
}
}
namespace {
use \some_namespace\MyClass as MyClass;
}
which doesn't throw any additional error but MyClass still isn't available in the global namespace (Class 'MyClass' not found). I don't quite understand why.
Put this in your global namespace:
use \some_namespace\MyClass as MyClass;
I believe it should work.
Edit: It does not. This should work:
class_alias('\some_namespace\MyClass', 'MyClass');

Categories