PHP code example of spaf / metamagic

1. Go to this page and download the library: Download spaf/metamagic library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

spaf / metamagic example snippets



use spaf\metamagic\abstract\AbstractSpell;
use spaf\metamagic\enums\TargetType;
use spaf\metamagic\MetaMagic;



#[Attribute(Attribute::TARGET_CLASS)]
class MyClassAttr {}

#[Attribute(Attribute::TARGET_ALL)]
class MyCommonAttr {}

#[MyClassAttr]
class MyClass {

	#[MyCommonAttr]
	public $my_field = null;

	#[MyCommonAttr]
	protected function myMethod() {

	}

}

$my_obj = new MyClass();

$filter = function ($spell): AbstractSpell | null {
	// Returning spell will ass],
	
	// The same about types it can be an array of enum values or a single enum value
	types: [TargetType::ClassType, TargetType::FieldType],
	
	// It allows to control whether you want to
	// search attributes with inheritance involved or exact attribute class.
	// is_instance_of is set to true by default - what means that it will consider inheritance
	// (children classes of an attribute as well)
	is_instance_of: true,
	
	// You can perform a custom filtering by providing a callable
	filter: $filter
);

$first_spell = MetaMagic::findSpellOne(
	// Refs param can contain array with objects, class-refs.
	// You also can directly specify a single ref without wrapping it into array
	refs: $my_obj,
	
	// The same about attrs it can be an array of class-refs or a single class-ref to an Attribute
	attrs: [MyClassAttr::class, MyCommonAttr::class],
	
	// The same about types it can be an array of enum values or a single enum value
	types: [TargetType::ClassType, TargetType::FieldType],
	
	// It allows to control whether you want to
	// search attributes with inheritance involved or exact attribute class.
	// is_instance_of is set to true by default - what means that it will consider inheritance
	// (children classes of an attribute as well)
	is_instance_of: true,
	
	// You can perform a custom filtering by providing a callable
	filter: $filter
);

print_r($first_spell);