Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Eregi i analiza funkcji
Bastion
post
Post #1





Grupa: Zarejestrowani
Postów: 505
Pomógł: 0
Dołączył: 8.01.2005

Ostrzeżenie: (0%)
-----


Witam, pisze sobie drobny skrypt do analizy skladni i budowy funkcji , narazie mam cos takiego :

  1. <?php
  2. $test = "function prefix_foo($input, $param_1 = 'value_1', $param_2 = 'va)lue,2', $param_3 = 345, $param_4)";
  3.  
  4. $eregi = 'function[ ]{1,}(prefix_[_0-9a-z]{1,})[ ]{0,}\([ ]{0,}'; // funkcja i nazawa
  5. $eregi.= '((\$[_]{0,1}[_a-z0-9]{1,}[ ]{0,}[=]{0,1}[ ]{0,}['_),a-z0-9']{0,}[ ]{0,}[,]{0,1}[ ]{0,}){0,10})'; // parametry
  6. $eregi.= '[)]{1}';
  7. print $eregi;
  8. if (eregi($eregi, $test, $foo))
  9. {
  10. print '<pre>';
  11. print_r($foo);
  12. print '</pre>';
  13. }
  14. ?>


wynik otrzymamy taki

Kod
Array
(
    [0] => function prefix_foo($input, $param_1 = 'value_1', $param_2 = 'va)lue,2', $param_3 = 345, $param_4)
    [1] => prefix_foo
    [2] => $input, $param_1 = 'value_1', $param_2 = 'va)lue,2', $param_3 = 345, $param_4
    [3] => $param_4
)


gdzie
[1] => nazwa funkcji
[2] => parametry
[3] => nie wiem skad sie to wzielo :/

i teraz nie mam pomyslu jak rozbic parametry na tablice, tak by okreslic czy parametr jest wymagany, czy opcjonalny z domyslna wartoscia.

Ten post edytował Bastion 22.04.2006, 11:54:27
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
dr_bonzo
post
Post #2





Grupa: Przyjaciele php.pl
Postów: 5 724
Pomógł: 259
Dołączył: 13.04.2004
Skąd: N/A

Ostrzeżenie: (0%)
-----


Nie odkrywasz ponownie kola?
(php >= 5)

http://www.php.net/manual/en/language.oop5.reflection.php
Cytat
The ReflectionFunction class lets you reverse-engineer functions.

<?php
class ReflectionFunction implements Reflector
{
  final private __clone()
  public object __construct(string name)
  public string __toString()
  public static string export(string name, bool return)
  public string getName()
  public bool isInternal()
  public bool isUserDefined()
  public string getFileName()
  public int getStartLine()
  public int getEndLine()
  public string getDocComment()
  public array getStaticVariables()
  public mixed invoke(mixed args)
  public mixed invokeArgs(array args)
  public bool returnsReference()
  public ReflectionParameter[] getParameters()
  public int getNumberOfParameters()
  public int getNumberOfRequiredParameters()
}
?>


Cytat
The ReflectionParameter class retrieves information about a function's or method's parameters.

<?php
class ReflectionParameter implements Reflector
{
  final private __clone()
  public object __construct(string name)
  public string __toString()
  public static string export(mixed function, mixed parameter, bool return)
  public string getName()
  public bool isPassedByReference()
  public ReflectionFunction getDeclaringFunction()
  public ReflectionClass getDeclaringClass()
  public ReflectionClass getClass()
  public bool isArray()
  public bool allowsNull()
  public bool isPassedByReference()
  public bool getPosition()
  public bool isOptional()
  public bool isDefaultValueAvailable()
  public mixed getDefaultValue()
}
?>
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 2.10.2025 - 16:45