CakeFest 2024: The Official CakePHP Conference

ReflectionMethod Sınıfı

(PHP 5, PHP 7, PHP 8)

Giriş

ReflectionMethod sınıfı bir yöntem hakkında bilgi edinilmesini sağlar.

Sınıf Sözdizimi

class ReflectionMethod extends ReflectionFunctionAbstract {
/* Sabitler */
public const int IS_STATIC;
public const int IS_PUBLIC;
public const int IS_PROTECTED;
public const int IS_PRIVATE;
public const int IS_ABSTRACT;
public const int IS_FINAL;
/* Özellikler */
public string $class;
/* Miras alınan özellikler */
public string $name;
/* Yöntemler */
public __construct(object|string $objectOrMethod, string $method)
public static createFromMethodName(string $method): static
public static export(string $sınıf, string $isim, bool $ihracet = false): string
public getClosure(?object $object = null): Closure
public getModifiers(): int
public getPrototype(): void
public hasPrototype(): bool
public invoke(?object $nesne, mixed ...$bağımsız_değişkenler): mixed
public invokeArgs(?object $nesne, array $bağımsız_değişkenler): mixed
public isAbstract(): bool
public isDestructor(): bool
public isFinal(): bool
public isPrivate(): bool
public isProtected(): bool
public isPublic(): bool
public setAccessible(bool $accessible): void
public __toString(): string
/* Miras alınan yöntemler */
}

Özellikler

name

Yöntemin ismi.

class

Sınıfın ismi

Öntanımlı Sabitler

ReflectionMethod Değiştiricileri

ReflectionMethod::IS_STATIC

Yöntemin statik olduğunu belirtir. PHP 7.4.0 öncesinde, bu değer 1 idi.

ReflectionMethod::IS_PUBLIC

Yöntemin public olduğunu belirtir. PHP 7.4.0 öncesinde, bu değer 256 idi.

ReflectionMethod::IS_PROTECTED

Yöntemin protected olduğunu belirtir. PHP 7.4.0 öncesinde, bu değer 512 idi.

ReflectionMethod::IS_PRIVATE

Yöntemin private olduğunu belirtir. PHP 7.4.0 öncesinde, bu değer 1024 idi.

ReflectionMethod::IS_ABSTRACT

Yöntemin abstract olduğunu belirtir. PHP 7.4.0 öncesinde, bu değer 2 idi.

ReflectionMethod::IS_FINAL

Yöntemin bir final olduğunu belirtir. PHP 7.4.0 öncesinde, bu değer 4 idi.

Bilginize:

Sabitlerin değerleri PHP sürümleri arasında farklılık gösterebilir. Bu bakımdan sabitler değerleriyle değil isimleriyle kullanılmalıdır.

Sürüm Bilgisi

Sürüm: Açıklama
8.0.0 ReflectionMethod::export() kaldırıldı.

İçindekiler

add a note

User Contributed Notes 3 notes

up
9
Anonymous
3 years ago
We can make a "Automatic dependenci injector" in classes when her constructors depends other classes (with type hint).

<?php

class Dependence1 {
function
foo() {
echo
"foo";
}
}

class
Dependence2 {
function
foo2() {
echo
"foo2";
}
}

final class
myClass
{
private
$dep1;
private
$dep2;

public function
__construct(
Dependence1 $dependence1,
Dependence2 $dependence2
)
{
$this->dep1 = $dependence1;
$this->dep2 = $dependence2;
}

}

// Automatic dependence injection (CLASSES)

$constructor = new ReflectionMethod(myClass::class, '__construct');
$parameters = $constructor->getParameters();

$dependences = [];
foreach (
$parameters as $parameter) {
$dependenceClass = (string) $parameter->getType();
$dependences[] = new $dependenceClass();
}

$instance = new myClass(...$dependences);
var_dump($instance);

?>

Results in:

object(myClass)#6 (2) {
["dep1":"myClass":private]=>
object(Dependence1)#4 (0) {
}
["dep2":"myClass":private]=>
object(Dependence2)#5 (0) {
}
}
up
12
webseiten dot designer at googlemail dot com
12 years ago
Note that the public member $class contains the name of the class in which the method has been defined:

<?php
class A {public function __construct() {}}
class
B extends A {}

$method = new ReflectionMethod('B', '__construct');
echo
$method->class; // prints 'A'
?>
up
-18
no dot prob at gmx dot net
17 years ago
I have written a function which returns the value of a given DocComment tag.

Full example:

<?php

header
('Content-Type: text/plain');

class
Example
{
/**
* This is my DocComment!
*
* @DocTag: prints Hello World!
*/
public function myMethod()
{
echo
'Hello World!';
}
}

function
getDocComment($str, $tag = '')
{
if (empty(
$tag))
{
return
$str;
}

$matches = array();
preg_match("/".$tag.":(.*)(\\r\\n|\\r|\\n)/U", $str, $matches);

if (isset(
$matches[1]))
{
return
trim($matches[1]);
}

return
'';
}

$method = new ReflectionMethod('Example', 'myMethod');

// will return Hello World!
echo getDocComment($method->getDocComment(), '@DocTag');

?>

Maybe you can add this functionality to the getDocComment methods of the reflection classes.
To Top