How to import static function with namespace in php? - php

class A declared at namespace1.
namesapce namesapce1;
class A
{
public static function fun1()
{
}
}
I want to use fun1() inside class B:
namespace namesapce2;
use ???? as fun1
class B
{
public static func2()
{
fun1();
}
}
How to do that ?

namespace namespace2;
use namespace1\A;
class B
{
public static function func2()
{
A::fun1();
}
}
Assuming you're using something that does autoloading or the necessary includes.

Related

PHP - Protected/Abstract method to use class declared in child class

I have an abstract class, in which I want to call method, from a class that is declared in the child (extending) class. An example looks like this:
The abstract class:
abstract class NumberGenerator
{
protected function generate($input){
return MyClass::MyMethod($input);
}
}
My child/extending class:
use TomsFolder\MyClass;
use MyFolder\NumberGenerator;
class TomsNumberGenerator extends NumberGenerator
{
public function generate(string $applicantId): string
{
return $this->generate();
}
}
Another child/extending class:
use DavesFolder\MyClass;
use MyFolder\NumberGenerator;
class DavesNumberGenerator extends NumberGenerator
{
public function generate(string $applicantId): string
{
return $this->generate();
}
}
So I want to call MyClass::MyMethod in NumberGenerator. However it is only imported in TomsNumberGenerator.
The reason I want to do it like is because, I have classes like DavesNumberGenerator which calls a different MyClass.
When I try this, I get 'MyClass is not found in NumberGenerator'. Is there any way to make this work?
Try putting the namespace use statement before the actual class:
NumberGenerator.php
use MyFolder\MyClass;
abstract class NumberGenerator
{
protected function generate($input){
return MyClass::MyMethod($input);
}
}
EDIT
Try this:
NumberGenerator.php
abstract class NumberGenerator
{
protected function generate($class_name, $input){
return call_user_func($class_name . '::MyMethod', $input);
}
}
TomsNumberGenerator.php
use TomsFolder\MyClass;
use MyFolder\NumberGenerator;
class TomsNumberGenerator extends NumberGenerator
{
public function generate(string $applicantId): string
{
return $this->generate(get_class(new MyClass()), $applicantId);
}
}
You have to use interface for this.
You can do the following
Create MyClassInterface
interface MyClassInterface {
public function MyMethod();
}
Implement this interface in some classes
class MyClass1 implements MyClassInterface {
public function MyMethod() {
// implementation
}
}
class MyClass2 implements MyClassInterface {
public function MyMethod() {
// implementation 2
}
}
Add abstract method to NumberGenerator
abstract class NumberGenerator {
abstract protected function GetMyClass(): MyClassInterface;
protected function generate($input){
return $this->GetMyClass()->MyMethod($input);
}
}
Implement GetMyClass function inside child classes
class TomsNumberGenerator extends NumberGenerator
{
protected function GetMyClass(): MyClassInterface {
return new MyClass1();
}
}
class DavesNumberGenerator extends NumberGenerator
{
protected function GetMyClass(): MyClassInterface {
return new MyClass2();
}
}
PS If you want to use static, you can change abstract inside NumberGenerator class, to change string. In this case, your generate will look like this:
protected function generate($input){
return call_user_func($this->GetMyClass() . '::MyMethod', [$input]);
}

Why relative static call not implement namespace of extended class?

Why only static call return 2?
it seems to me that a class call not by absolute name should depend on the current namespace in the class
<?php
namespace A {
class B {
static function test(){
echo 1;
}
static function check(){
B::test();//1 why?
self::test();//1
static::test();//2
}
}
}
namespace B {
class B extends \A\B {
static function test(){
echo 2;
}
}
}
namespace {
B\B::check();
}
B::test() is executed inside the A namespace and therefore takes the B class, that is provided by the A namespace. Therefore A\B::test is called. The context of the current class is not relevant for that.
The following example shows, that the heredity is irrelevant for this behavior.
<?php
namespace A {
class B {
static function test() {
echo 3;
}
}
class A {
static function test(){
echo 1;
}
static function check(){
B::test();//3
self::test();//1
static::test();//2
}
}
}
namespace B {
class B extends \A\A {
static function test(){
echo 2;
}
}
}
namespace {
B\B::check();
}
static::test returns 2 eventhough you are in the A namespace, because static takes the context from the calling super class and not the current namespace.
self::test returns 1, because self takes the context of the current class instead of the super class.

How to assign a trait method to child class object?

my question is very easy.I have a trait and child class.
trait mixVar {
public function go(){
return [];
}
}
class example extends foo {
use mixVar;
public $var=$this->go(); //does not work
}
how to make this in php?
You cannot use a method call to assign a default value to a property. You would have to do it in the constructor.
class example extends foo
{
use mixVar;
public $var;
public function __construct()
{
$this->var = $this->go();
}
}
Another solution is to define trait method like:
trait mixVar {
public function go(){
$this->var = []; // your value here
}
}

Php Class & Trait methods with same name

I have this specific situation, my trait has a method and my class has a method, both with same name.
I need to use both methods (the one from the trait and the class) Inside that class which contains that same method
namespace Some\Namespace;
use Some\Other\Namespace\TestTrait;
class TestClass {
use TestTrait;
public function index()
{
// should call the method from the class $this->getId();
// should also call the method from the trait $this->getId();
}
private function getId()
{
// some code
}
}
And in seperate defined Trait:
trait TestTrait
{
private function getId ()
{
// does something
}
}
Please note this is not pasted code, I might have some typos :P
Use trait Conflict Resolution
namespace Some\Namespace;
use Some\Other\Namespace\TestTrait;
class TestClass {
use TestTrait {
getId as traitGetId;
}
public function index()
{
$this->getId();
$this->traitGetId();
}
private function getId()
{
// some code
}
}

Call parent static method in php

I have a base class A:
class A {
public static function a() {
...
}
public static function b() {
...
}
}
and an extended class B
class B extends A {
public static function a() {
...
}
public static function c() {
...
}
}
I would like to be able to call all the methods using B::
How would I call A::b, using B::?
You should be able to accomplish this as easily as:
class B extends A {
public static function a() {
parent::a();
}
}
See the docs

Categories