PHP Attributes Reader
  • Introduction
  • Installation
  • Reading Attributes
    • Quick Start
    • Class Attributes
    • Method Attributes
    • Property Attributes
    • Attribute Class
    • Argument Class
  • Limitations
  • Contribution
Powered by GitBook
On this page
  1. Reading Attributes

Method Attributes

PHP Class with Class Attributes

<?php

declare(strict_types=1);

class Abc
{
    #[TestAttribute11(["key1" => "key1 value"])]
    #[TestAttribute12(10.13)]
    #[TestAttribute13(123)]
    #[TestAttribute14()]
    #[TestAttribute15(["value1", "value2"])]
    public function function1() {
        // rest of the codes
    }
    // rest of the codes
}

Initialize the Attributes Reader

<?php

declare(strict_types=1);

use \AntonDPerera\PHPAttributesReader\AttributesReader;

$class = Abc::class;

$attributes_reader = AttributesReader($class);

Available functions for Method Attributes

public function hasMethodAttributes(?string $method_name = null): bool
  1. If $method_name is null, returns true or false based on availability of Method attributes in the class.

  2. If $method_name is not null, returns true or false based on availability of Method attributes for the given $method_name.

public function getMethodAttributes(?string $method_name = null): array | MethodNotFoundException
  1. If $method_name is null, returns all the Method attributes in the class.

  2. If $method_name is not null, returns all the Method attributes for the given method name. It will be an array of attributes. Each item on Array will be an instance of Attribute class

    \AntonDPerera\PHPAttributesReader\Attribute
  3. If given $method_name is not available within the list of Method attributes, this function will throws AntonDPerera\PHPAttributesReader\Exceptions\MethodNotFoundException.

public function getMethodAttribute(string $method_name, string $attribute_name): Attribute | MethodAttributeNotFoundException
  1. If given $method_name is not available within the list of Method attributes, this function will throws AntonDPerera\PHPAttributesReader\Exceptions\MethodNotFoundException.

  2. If given $method_name is available within the list of Method attributes but the given $attribute_name doesn't exists in the attributes list this function will throws AntonDPerera\PHPAttributesReader\Exceptions\MethodAttributeNotFoundException.

PreviousClass AttributesNextProperty Attributes

Last updated 1 year ago

Refer for more details.

If given $method_name is available within the list of Method attributes and the given $attribute_name exists too for that method, this function will return an instance of Attribute class. Refer for more details.

Attribute class Reference documentation
Attribute class Reference documentation