Search
 
SCRIPT & CODE EXAMPLE
 

PHP

php 8 attributes

      /*
      in MyClassContainingAttributesOnMethods:
      */
      class MyClassContainingAttributesOnMethods
      {
      	#[MyAttribute]
		#[MyOtherAttribute("someArgument", 1234, "someOtherArguments")]
		public function someFunction() {...}

      }
      
      /*
      in MyOtherAttribute:
      */
	  #[Attribute]
      class MyOtherAttribute
      {  
      	public function __construct(string $arg1, int $arg2, string $arg3) {...}
      }
      
	  /*
      In app:
      */
       $reflectionClass = new ReflectionClass(MyClassContainingAttributesOnMethods::class);

        foreach($reflectionClass->getMethods() as $method) {
            $attributes = $method->getAttributes(MyAttribute::class);
            if(count($attributes) == 1) {
                $method = MyClassContainingAttributesOnMethods::class."::".$method->getName();
                $arguments = [];
                foreach($method->getAttributes(MyOtherAttribute::class) as $otherArgument) {
                    $otherArg = $otherArgument->newInstance();
                    $arguments[$otherArg->argument] = self::require($otherArgument->serviceId);
                }

                $result = $method(...$arguments);
            }
        }
Comment

PHP 8 Attributes

#[Attribute]
class ListensTo
{
    public string $event;

    public function __construct(string $event)
    {
        $this->event = $event;
    }
}
Comment

PREVIOUS NEXT
Code Example
Php :: update onlu one column laravel 
Php :: update pdo mysql php 
Php :: eloquent model sort by ascending order 
Php :: wordpress show notice 
Php :: php ziparchive compress folder 
Php :: message mkdir() invalid path filename drivers/session_files_driver.php 
Php :: page expire in laravel 
Php :: laravel log daily 
Php :: laravel model create array 
Php :: how to publish stubs in laravel 
Php :: laravel base64 decode save file 
Php :: laravel get current action name 
Php :: Format and show date PHP 
Php :: php upload from url 
Php :: laravel migration remove relationship from table 
Php :: laravel 8 change password 
Php :: php regex file extension 
Php :: php trim string if longer than 
Php :: e_notice in php 
Php :: twig filter line break 
Php :: PHP Simple HTML DOM 
Php :: wp create user programmatically 
Php :: how-to-call-ajax-in-wordpress 
Php :: how to get local current time in laravel 
Php :: ::update() should not be called statically 
Php :: php replace blackslash 
Php :: add log in laravel 
Php :: strcasecmp php 
Php :: mpdf output 
Php :: create foreign key laravel migration 
ADD CONTENT
Topic
Content
Source link
Name
1+5 =