Overwrite namespace usage in extended class in PHP - 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.

Related

Calling static class outside of namespace

Inside a class "cl1" in the namespace "space1", there is the function "fctn1", and inside it I want to call the static method "fctn2" from the class "cl2". The class cl2 is not inside any namespaces.
<?php
namespace space1;
class cl1
{
public function fctn1()
{
cl2::fctn2();
}
}
<?php
class cl2
{
public static function fctn2()
{
// stuff here
}
}
How can I call fctn2 inside the fctn1 on PHP?
I tryied simply calling the method inside the class just like shown in the code on the question.
Honk der Hase gave a satisfactory answer in a comment:
Prepend a backslash to address the global namespace:
\cl2::fetch2();

Laravel/PHP Refactor code to use same different modal

I am using Laravel (PHP). I want to refactor below code to ask class A and class B to use the same funsth(). funSth() is repeated in two class while the only difference is the model.
Is there a way I can simplify to below? Is that possible to refactor it into second code?
Current:
class A {
public function funSth(){
Models\Profile::create();
}
public function main(){
$this->funA();
}
}
class B {
public function funSth(){
Models\Location::create();
}
public function main(){
$this->funB();
}
}
Expected:
class A {
public function main(){
(new Y)->funSth();
}
}
class B {
public function main(){
(new Y)->funSth();
}
}
class Y {
public function funSth(){
Models\AnyModel::create();
}
}
One way you could do this is to make a Trait. Traits essentially allow you to use the same code in multiple classes.
Base on the code in your question, your trait would look something like:
trait Y
{
public function funSth()
{
Models\AnyModel::create();
}
}
Don't forget to add the correct namespace for the file.
Then in your other classes you would have the use the use keyword inside the class body:
class A
{
use Y;
public function main()
{
$this->funA();
}
}
If you're trait is in a different namespace then you'll need to either give the full class name with the namespace or import the trait into your class.
After researching for a while. Here is my solution.
use Illuminate\Database\Eloquent\Model;
trait Y
{
protected function create(Model $model, array $attributes){
model->create($attributes);
}
}
Therefore, for Class A and Class B:
class A
{
use Y;
public function main($data){
$this->create((new Models\ModelAA), $data)
}
}
class B
{
use Y;
public function main($data){
$this->create((new Models\ModelBB), $data)
}
}
The basic idea is to use the abstract class Illuminate\Database\Eloquent\Model in trait Y and initialize model before it.

Call a child class from a child class PHP

i am a new PHP, i have some files like this:
In root.php i have
<?php
class Root
{
// Some functions here
}
In child_1.php i have:
class Child_1 extends Root
{
function abc()
{
//Some code here
}
}
In child_2.php i have:
class Child_2 extends Root
{
function cde()
{
// How can i call function abc() from class Child_1 here
}
}
Yes, how can i call a function of Child_1 class from a function of Child_2 class.
As classes that extend one parent (Root) know nothing about each other (and you cannot change this), you can do:
class Chirld_2 extends Root
{
function cde()
{
$obj = new Chirld_1();
$obj->abc();
}
}
But obviously it's a lack of good design. I suppose, you have to define abc() in a Root class to make it available in both children:
class Root
{
function abc()
{
//Some code here
}
}
class Chirld_1 extends Root
{
}
class Chirld_2 extends Root
{
}
$c1 = new Chirld_1();
$c1->abc();
$c2 = new Chirld_2();
$c2->abc();
you can call any function in class_1 or Class_2 of parent class
but within any child class you can't call method from another.because They do not have a relevance
a solution is you call class_2 in constructor of class_1
If you want to use functions of Chirld_1 from Chirld_2 or vice versa without creating an object of the child class, then one option is to declare Chirld_1 and Chirld_2 as traits of the parent class Root. This will allow you to call functions of Chirld_1 from Chirld_2 and vice verse using the $this keyword. The classes should be declared as follows:
<?php
trait Chirld_1
{
public function test1()
{
$this->test2();
}
}
trait Chirld_2
{
public function test2()
{
$this->test1();
}
}
class Root
{
use Chirld_1;
use Chirld_2;
// Some functions here
}
See this links for more information about traits: http://php.net/manual/en/language.oop5.traits.php
Try using parent:: or static:: or self:: notation to designate the class you want to use.
http://php.net/manual/pl/keyword.parent.php

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"); ?

Php: Overriding abstract method goes wrong

I think there is a problem in php's OOP implementation.
EDIT: Consider more illustrative example:
abstract class Animal {
public $name;
// public function Communicate(Animal $partner) {} // Works
public abstract function Communicate(Animal $partner); // Gives error
}
class Panda extends Animal {
public function Communicate(Panda $partner) {
echo "Hi {$partner->name} I'm a Panda";
}
}
class Human extends Animal {
public function Communicate(Human $partner) {
echo "Hi {$partner->name} I'm a Human";
}
}
$john = new Human(); $john->name = 'John';
$mary = new Human(); $mary->name = 'Mary';
$john->Communicate($mary); // should be ok
$zuzi = new Panda(); $zuzi->name = 'Zuzi';
$zuzi->Communicate($john); // should give error
The problem is that when Animal::Communicate is an abstract method, php tells that the following methods are illegal:
"public function Communicate(Panda $partner)"
"public function Communicate(Human $partner)"
but when Animal::Communicate is non-abstract but has zero-implementation Php thinks that these methods are legal. So in my opinion it's not right because we are doing override in both cases, and these both cases are equal, so it seems like it's a bug...
Older part of the post:
Please consider the following code:
Framework.php
namespace A
{
class Component { ... }
abstract class Decorator {
public abstract function Decorate(\A\Component $component);
}
}
Implementation.php
namespace B
{
class MyComponent extends \A\Component { ... }
}
MyDecorator.php
namespace A
{
class MyDecorator extends Decorator {
public function Decorate(\B\MyComponent $component) { ... }
}
}
The following code gives error in MyDecorator.php telling
Fatal error: Declaration of MyDecorator::Decorate() must be compatible with that of A\Decorator::Decorate() in MyDecorator.php on line ...
But when I change the Framework.php::Decorator class to the following implementation:
abstract class Decorator {
public function Decorate(\A\Component $component) {}
}
the problem disappears.
I'm not sure (haven't tested it ;), but you declare this abstract function:
public abstract function Decorate(\A\Component $component);
So you should implement this EXACTLY like that. But you did this:
public function Decorate(\B\MyComponent $component) { ... }
That's not the same. Could you try to change that to \A\Component?
To all comments: fact of the matter is that this piece of PHP "runs"
namespace A
{
class Component { }
abstract class Decorator {
public abstract function Decorate(\A\Component $component);
}
}
namespace B
{
class MyComponent extends \A\Component { }
}
namespace A
{
class MyDecorator extends Decorator {
public function Decorate(\A\Component $component) {}
}
}
And this doesn't:
<?php
namespace A
{
class Component { }
abstract class Decorator {
public abstract function Decorate(\A\Component $component);
}
}
namespace B
{
class MyComponent extends \A\Component { }
}
namespace A
{
class MyDecorator extends Decorator {
public function Decorate(\B\MyComponent $component) {}
}
}
?>
With this error: PHP Fatal error: Declaration of A\MyDecorator::Decorate() must be compatible with that of A\Decorator::Decorate() in line 18
Now you can discuss all you like about how that should or should not be, but that's the problem with the code.
so, to satisfy my own curiosity: this is illegal too:
<?php
class Component { }
abstract class Decorator {
public abstract function Decorate(Component $component);
}
class MyComponent extends Component { }
class MyDecorator extends Decorator {
public function Decorate(MyComponent $component) {}
}
?>
It's not the namespaces or anything. It just doesn't seem legal.
See http://bugs.php.net/bug.php?id=36601, this issues has been reported as a bug but was rejected because of laziness :D
It has nothing to do with it being abstract. It has to do with the type hinting. The two definitions of the method are not compatible because you explicitly set the argument to be of type \A\Component and then try to overload the method with \B\Component you cant do that because it changes the method signature. Any subsequent declaration of Decorate must use the same type hint as its parent declaration in order for the method signatures to be compatible.
This might assist someone, and am not late.
The best way to handle such is by using an interface.
Consider below;
<?php
interface Componentor{}
class Component implements Componentor { }
abstract class Decorator {
public abstract function Decorate(Componentor $component);
}
class MyComponent extends Component { }
class MyDecorator extends Decorator {
public function Decorate(Componentor $component) {}
}
?>
Usage;
<?php
$c=new Component();
//TODO ....blah blah blah...
$decor=new MyDecorator();
//TODO ....blah blah blah...
$decor->Decorate($c);
?>

Categories