Alle methods van een class uitlezen en uitvoeren

Overzicht Reageren

Sponsored by: Vacatures door Monsterboard

- Ariën  -
Beheerder

- Ariën -

14/04/2018 17:16:18
Quote Anchor link
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:

Quote:
Fatal error: Uncaught Error: Access to undeclared static property: myClass::$method_name in /bla/scriptje.php:29


Code (php)
PHP script in nieuw venster Selecteer het PHP script
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

<?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?
 
PHP hulp

PHP hulp

29/03/2024 07:54:17
 
Thomas van den Heuvel

Thomas van den Heuvel

14/04/2018 17:32:53
Quote Anchor link
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.
Gewijzigd op 14/04/2018 18:05:15 door Thomas van den Heuvel
 
- Ariën  -
Beheerder

- Ariën -

14/04/2018 17:34:07
Quote Anchor link
Kijk, dat zou mooi zijn! :-)

Ik ga weer even verder klussen!
 
Thomas van den Heuvel

Thomas van den Heuvel

14/04/2018 17:35:48
Quote Anchor link
Mogelijk werkt $method_name() ook?
Gewijzigd op 14/04/2018 17:39:07 door Thomas van den Heuvel
 
- Ariën  -
Beheerder

- Ariën -

14/04/2018 17:38:26
Quote Anchor link
Jep, toch wel :-)
 
Rob Doemaarwat

Rob Doemaarwat

14/04/2018 18:38:19
Quote Anchor link
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
 
Ward van der Put
Moderator

Ward van der Put

14/04/2018 19:46:17
Quote Anchor link
Ik gebruik een variant waarbij argumenten optioneel zijn. Ingekort:

Code (php)
PHP script in nieuw venster Selecteer het PHP script
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
<?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;
    }
}

?>


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)
PHP script in nieuw venster Selecteer het PHP script
1
2
3
4
<?php
        $controller
= $this->Controller;
        $thread = new $controller();
?>
 
Ozzie PHP

Ozzie PHP

15/04/2018 00:58:00
Quote Anchor link
Aar, even voor de zekerheid ... deze ken je toch?

http://php.net/manual/en/class.reflectionclass.php
 



Overzicht Reageren

 
 

Om de gebruiksvriendelijkheid van onze website en diensten te optimaliseren maken wij gebruik van cookies. Deze cookies gebruiken wij voor functionaliteiten, analytische gegevens en marketing doeleinden. U vindt meer informatie in onze privacy statement.