Home > Software design >  Is there a PHP namespace extension that allows you to import/use/alias functions as if it was extend
Is there a PHP namespace extension that allows you to import/use/alias functions as if it was extend

Time:12-13

Is there a PHP namespace extension that allows you to import/use/alias functions as if it was extended directly into the class rather your typical namespace'd silo?

main.php

<?php 
namespace api;

class main extends core{
  print $this->whoami; // prints computer
}

core.php i.e. class core holding basic functions / standards:

<?php 
namespace api;

class core{
  function __construct(){
    $this->whoami = "computer";
  }
}

I want to add modular like classes and or functions that can be directly access from the class i.e.,

function-addon.php:

<?php 
namespace api;
    
function abc($a){
 print $a;
}

function-class.php

<?php 
namespace api;

class tools{
  function tool_a( $a ){
     return $a;
  }  
}

with main.php looking like this (non working sample below):

<?php 
namespace api;

use function api\abc as abc; 
use api\tools as tools;

class main extends core{
  print $this->whoami; // prints computer
  
  print $this->abc(5); // print 5 (desired access)
  
  print tools::tool_a(10); //print 10

}

The goal is "$this->abc" access and not i.e., tools:tool_a.

CodePudding user response:

Yes, there is a PHP namespace extension that allows you to import, use, and alias functions as if they were directly extended into a class. This extension is called the "use" keyword, and it allows you to specify the namespace and class name of the functions you want to use, and then use them as if they were part of the current class. Here is an example of how you might use the "use" keyword to import and use functions from another namespace in your PHP code:

namespace MyNamespace;

use OtherNamespace\MyClass;

class MyClass {
  public function myFunction() {
    // Use a function from OtherNamespace\MyClass as if it was part of this class
    MyClass::myFunction();
  }
}

In this example, the MyClass class in the MyNamespace namespace uses the use keyword to import the MyClass class from the OtherNamespace namespace. It then uses the MyClass::myFunction() method as if it were a part of the MyClass class in the MyNamespace namespace. This allows you to use functions from other namespaces in your code without having to fully qualify their namespace and class name each time you use them.

CodePudding user response:

As I experiment I have a working solution, but I'm not a huge fan of yet as it doesn't use namespaces / aliases.

<?php 
namespace api;

require("function-addon.php");

class main extends core{
}

function-addon.php:

<?php 
    
function abc($a){
 print $a;
}

Which would then allow the following to work:

<?php 
namespace api;

require("function-addon.php");

class main extends core{
  
   function __call( $func, $arg ){
     return $this->func($arg);
   }

}

The above allows the magic __call function access the locally referenced function-addon.php file.

CodePudding user response:

The solution here is PHP Traits (https://www.php.net/manual/en/language.oop5.traits.php)

Per @nice_dev reference, a PHP trait implements a way to reuse code.

function-addon.php:

<?php
namespace api;

trait tools{
  function abc($a;){
   return $a;
  }
}

Which would then allow the following to work:

<?php 
namespace api;

require("function-addon.php");

class main extends core{

   use tools;
  
   function __construct(){
      $this->abc(5); //returns 5
   }

}

Make sure you use namespace in your traits!

  •  Tags:  
  • php
  • Related