Friday, July 22, 2022

[FIXED] How to import static function with namespace in php?

Issue

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 ?


Solution

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.



Answered By - ChadSikorra
Answer Checked By - Mary Flores (PHPFixing Volunteer)

No comments:

Post a Comment

Note: Only a member of this blog may post a comment.