Skip to content

Instantly share code, notes, and snippets.

@diloabininyeri
Created October 28, 2022 10:09
Show Gist options
  • Select an option

  • Save diloabininyeri/00df0143eabdcdd4436d9966ac29a266 to your computer and use it in GitHub Desktop.

Select an option

Save diloabininyeri/00df0143eabdcdd4436d9966ac29a266 to your computer and use it in GitHub Desktop.
php call dynamic method event that doesnt exist in object ...
<?php
/**
*
*/
#[Attribute(Attribute::TARGET_PROPERTY)]
class Name
{
private string $name;
/**
* @return void
*/
public function setName(string $name)
{
$this->name = $name;
}
/**
* @return string
*/
public function getName(): string
{
return $this->name;
}
}
/**
*
*/
#[Attribute(Attribute::TARGET_PROPERTY)]
class Job
{
/**
* @var string
*/
private string $job;
/**
* @param string $job
* @return void
*/
public function setJob(string $job): void
{
$this->job = $job;
}
/**
* @return string
*/
public function getJob(): string
{
return $this->job;
}
}
/**
*
*/
trait DynamicMethod
{
/**
* @var array
*/
private array $objects = [];
/**
* @throws ReflectionException
*/
public function __call(string $method, array $arguments)
{
{
$PropertyPrefixes = ['set', 'get'];
foreach ($PropertyPrefixes as $propertyPrefix) {
if (str_starts_with($method, $propertyPrefix)) {
return $this->callAttributeMethod($method, $propertyPrefix, $arguments);
}
}
}
}
/**
* @param string $propertyName
* @return void
* @throws ReflectionException
*/
private function associatePropertyInstance(string $propertyName): void
{
if (!isset($this->objects[$propertyName])) {
$this->objects[$propertyName] = $this->getAttributeInstance($propertyName);
}
}
/**
* @throws ReflectionException
*/
private function getAttributeInstance(string $propertyName): object
{
$reflection = new ReflectionProperty($this, $propertyName);
return $reflection->getAttributes()[0]->newInstance();
}
/**
* @param string $method
* @return string
*/
private function findPropertyName(string $method, string $setOrGet): string
{
$removeSet = str_replace($setOrGet, '', $method);
return lcfirst($removeSet);
}
/**
* @param string $method
* @param string $setOrGet
* @param array $arguments
* @return mixed
* @throws ReflectionException
*/
public function callAttributeMethod(string $method, string $setOrGet, array $arguments): mixed
{
$propertyName = $this->findPropertyName($method, $setOrGet);
$this->associatePropertyInstance($propertyName);
return $this->objects[$propertyName]->$method(...$arguments);
}
}
/**
*
* @method self setName(string $name)
* @method self setJob(string $job)
* @method string getJob()
* @method string getName()
*/
class Test
{
use DynamicMethod;
/**
* @var string
*/
#[Name]
private string $name;
/**
* @var string
*/
#[Job]
private string $job;
}
$test = new Test();
$test->setJob('developer');
echo $test->getJob();
echo PHP_EOL;
$test->setName('Dılo sürücü');
echo $test->getName();
echo PHP_EOL;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment