Alle methods van een class uitlezen en uitvoeren
Van de week kreeg ik een idee om de template-parser in mijn CMS te voorzien van een speciale functie-class waarbij zodat ik de functies voor mijn site eenvoudig mooi in methods in een class kan onderscheiden. Ik heb al geprobeerd om zoiets te bouwen, maar blijkbaar is PHP niet zo intelligent om dit te begrijpen:
<?php
class myClass {
// constructor
public static function __constructor()
{
return "Ik ben de constructor";
}
// method 1
public static function myFunction2()
{
return "Functie 1";
}
// method 2
public static function myFunction2()
{
return "Functie 2";
}
}
$class_methods = get_class_methods('myClass');
print_r($class_methods);
foreach ($class_methods as $method_name) {
echo myClass::$method_name;
}
?>
Zijn er mogelijkheden zodat je de waardes van de methods in een variabele kan plaatsen?
Quote:
Fatal error: Uncaught Error: Access to undeclared static property: myClass::$method_name in /bla/scriptje.php:29
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
class myClass {
// constructor
public static function __constructor()
{
return "Ik ben de constructor";
}
// method 1
public static function myFunction2()
{
return "Functie 1";
}
// method 2
public static function myFunction2()
{
return "Functie 2";
}
}
$class_methods = get_class_methods('myClass');
print_r($class_methods);
foreach ($class_methods as $method_name) {
echo myClass::$method_name;
}
?>
Zijn er mogelijkheden zodat je de waardes van de methods in een variabele kan plaatsen?
Yes we can! Voor het aanroepen-van-een-dynamische-static-method-naam zou je call_user_func() kunnen gebruiken.
Maar misschien moest je hier nog () achter gooien? Er staat "property", het is geen property, maar een methode.
Maar misschien moest je hier nog () achter gooien? Er staat "property", het is geen property, maar een methode.
Gewijzigd op 14/04/2018 18:05:15 door Thomas van den Heuvel
Kijk, dat zou mooi zijn! :-)
Ik ga weer even verder klussen!
Ik ga weer even verder klussen!
Mogelijk werkt $method_name() ook?
Gewijzigd op 14/04/2018 17:39:07 door Thomas van den Heuvel
Jep, toch wel :-)
Als je ook de argumenten wil weten kun je eens kijken naar Reflection: http://php.net/manual/en/class.reflectionparameter.php en het voorbeeld hiero http://php.net/manual/en/class.reflectionfunction.php
Ik gebruik een variant waarbij argumenten optioneel zijn. Ingekort:
Zonder deze kromme bocht kreeg ik het destijds niet aan de praat, maar misschien werkt het inmiddels beter in een nieuwe versie van PHP:
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
<?php
/**
* HMVC Route
*/
class Route
{
/**
* @var string $Controller
* @var null|string $Method
* @var null|array $Parameters
* @var string $Path
*/
private $Controller;
private $Method;
private $Parameters;
private $Path;
/**
* @param string $path
* @param string $controller
* @param string|null $method
* @param mixed|null $parameters
* @return self
*/
public function __construct($path, $controller, $method = null, $parameters = null)
{
$this->setPath($path);
$this->setController($controller);
if ($method !== null) {
$this->setMethod($method);
if ($parameters !== null) {
$this->setParameters($parameters);
}
}
}
/**
* Instantiate a controller and optionally call a method.
*
* @param void
* @return void
*/
public function dispatch()
{
$controller = $this->Controller;
$thread = new $controller();
if ($this->Method !== null) {
$method = $this->Method;
if ($this->Parameters !== null) {
$thread->$method($this->Parameters);
} else {
$thread->$method();
}
}
}
/**
* @param void
* @return string
*/
public function getPath()
{
return $this->Path;
}
/**
* @param string $controller
* @return void
*/
private function setController($controller)
{
$this->Controller = $controller;
}
/**
* @param string $method
* @return void
*/
private function setMethod($method)
{
$this->Method = $method;
}
/**
* @param mixed $parameters
* @return void
*/
private function setParameters($parameters)
{
$this->Parameters = $parameters;
}
/**
* @param string $path
* @return void
*/
private function setPath($path)
{
$path = '/' . ltrim($path, '/');
$path = mb_strtolower($path, 'UTF-8');
$this->Path = $path;
}
}
?>
/**
* HMVC Route
*/
class Route
{
/**
* @var string $Controller
* @var null|string $Method
* @var null|array $Parameters
* @var string $Path
*/
private $Controller;
private $Method;
private $Parameters;
private $Path;
/**
* @param string $path
* @param string $controller
* @param string|null $method
* @param mixed|null $parameters
* @return self
*/
public function __construct($path, $controller, $method = null, $parameters = null)
{
$this->setPath($path);
$this->setController($controller);
if ($method !== null) {
$this->setMethod($method);
if ($parameters !== null) {
$this->setParameters($parameters);
}
}
}
/**
* Instantiate a controller and optionally call a method.
*
* @param void
* @return void
*/
public function dispatch()
{
$controller = $this->Controller;
$thread = new $controller();
if ($this->Method !== null) {
$method = $this->Method;
if ($this->Parameters !== null) {
$thread->$method($this->Parameters);
} else {
$thread->$method();
}
}
}
/**
* @param void
* @return string
*/
public function getPath()
{
return $this->Path;
}
/**
* @param string $controller
* @return void
*/
private function setController($controller)
{
$this->Controller = $controller;
}
/**
* @param string $method
* @return void
*/
private function setMethod($method)
{
$this->Method = $method;
}
/**
* @param mixed $parameters
* @return void
*/
private function setParameters($parameters)
{
$this->Parameters = $parameters;
}
/**
* @param string $path
* @return void
*/
private function setPath($path)
{
$path = '/' . ltrim($path, '/');
$path = mb_strtolower($path, 'UTF-8');
$this->Path = $path;
}
}
?>
Zonder deze kromme bocht kreeg ik het destijds niet aan de praat, maar misschien werkt het inmiddels beter in een nieuwe versie van PHP:
Aar, even voor de zekerheid ... deze ken je toch?
http://php.net/manual/en/class.reflectionclass.php
http://php.net/manual/en/class.reflectionclass.php




