[PHP] frage bei bootstrap, event -> methoden aufbau

Dieses Thema im Forum "Webentwicklung" wurde erstellt von onip, 29. Mai 2012 .

  1. 29. Mai 2012
    Zuletzt bearbeitet: 29. Mai 2012
    frage bei bootstrap, event -> methoden aufbau

    hallo,

    hab ähnlichen code wie folgt vorliegen und versuch hinter der logik und mehrwert durchzusteigen:

    PHP:
    class  Home_Bootstrap  extends  Bootstrap
    {
        public function 
    install () {
            
    $event  $this -> addEvent (
                
    'postDispatch' ,
                
    'onPostDispatch'
            
    );
            
    $this -> registerEvent ( $event );
            return 
    true ;
        }
        
        static function 
    onPostDispatch ( Event_EventArgs $args ){    
            
    $request  $args -> methode ()-> getRequest ();
            
    print_r ( $request );
        }
    }
    der ablauf ist mir klar.
    es wird ein event angehängt der bei "postDispatch" die methode "onPostDispatch" ausführen soll.
    was ich nicht verstehe und wie man das nachbauen kann ist die werte übergabe von "onPostDispatch".
    PHP:
    static function  onPostDispatch ( Event_EventArgs $args ){
    ...
    wie kommt "Event_EventArgs $args" zu stande?
    könntet ihr mir ein kleines tut mach um den ablauf mitzubekommen und zu verstehen?

    danke
     
  2. 29. Mai 2012
    AW: frage bei bootstrap, event -> methoden aufbau

    Richte dir doch einfach einen Debugger ein. Bspw. in Netbeans mit xdebug. Wie das geht ist steht im Netz, dann kannst du einfach jede Zeile PHP mit durchgehen und dir die Werte der Variablen live anschauen.
     
    1 Person gefällt das.
  3. 30. Mai 2012
    AW: frage bei bootstrap, event -> methoden aufbau

    ich will wissen wie das geht und nicht was ankommt.
    oder ne zusatzfrage, wie nennt man solch einen aufbau
    PHP:
    ... ( klasse_methode_von_irgendwo $rückgabewert
    bei den parameterteil einer methode?
     
  4. 30. Mai 2012
    AW: frage bei bootstrap, event -> methoden aufbau

    Das könnte dir helfen:

    PHP 5 supports forced argument types
     
    1 Person gefällt das.
  5. 30. Mai 2012
    AW: frage bei bootstrap, event -> methoden aufbau

    perfekt, danke, jetzt hab ich's
    PHP:
    class  args  {
        public 
    $args  = array();
        public function 
    __construct (){
            
    $this -> setAr ( 'k0' '0' );
        }
        public function 
    setAr  ( $key $value ){
            
    $this -> args [ $key ] =  $value ;
        }
    }

    class 
    ext  extends  args  {    
        public function 
    test  ( args $args ){
            
    $args -> setAr ( 'k0' '1' );
            
    print_r ( $args );
        }
    }

    $test  = new  ext ();
    $test -> test (new  args );

    so wie es verstanden habe ist es nix anderes wie ein typen zuordnung.
    ähnlich wie
    PHP:
    $str  = (string)  123 ;
    var_dump ( $str );
    nur das eine instanz übergeben wird.

    was ich mich frage, ist der nutzen daraus.
    was spricht gegen so eine variante?
    PHP:
    class  args2  {
        public 
    $args  = array();
        public function 
    __construct (){
            
    $this -> setAr ( 'k0' '0' );
        }
        public function 
    setAr  ( $key $value ){
            
    $this -> args [ $key ] =  $value ;
        }
    }

    class 
    ext2  extends  args2  {    
        public function 
    test  (){
            
    $this -> setAr ( 'k0' '1' );
            
    print_r ( $this -> args );
        }
    }

    $test  = new  ext2 ();
    $test -> test ();

     
  6. 30. Mai 2012
    AW: frage bei bootstrap, event -> methoden aufbau

    damit erzwingst du eine bestimmte klasse (oder typ) als parameter, was dir lästiges typechecking in der logik erspart.

    dein zweites beispiel schimpft sich vererbung (schonmal gehört? ^^) und hat nichts mit der eigl. frage zu tun. da es ja um einen parameter geht.

    wäre die "onPostDispatch" methode nicht statisch deklariert, könnte man auch auf "$this" zurückgreifen, aber wer weiß wo genau der event aufgerufen wird?
     
    1 Person gefällt das.
  7. 30. Mai 2012
    Zuletzt bearbeitet: 30. Mai 2012
    AW: frage bei bootstrap, event -> methoden aufbau

    hab noch was rumgespielt.
    PHP:
    echo  "V1\n\r" ;
    class 
    args  {
        public 
    $args  = array();
        public function 
    __construct (){
            
    $this -> setAr ( 'k0' '0' );
        }
        public function 
    setAr  ( $key $value ){
            
    $this -> args [ $key ] =  $value ;
        }
    }

    class 
    ext  extends  args  {    
        public function 
    test  ( args $args ){
            
    $args -> setAr ( 'k0' '1' );
            
    print_r ( $args );
        }
    }

    $args  = new  args ();
    $test  = new  ext ();
    $test -> test ( $args );
    print_r ( $args );
    // anders
    echo  "V2\n\r" ;
    class 
    args2  {
        public 
    $args  = array();
        public function 
    __construct (){
            
    $this -> setAr ( 'k0' '0' );
        }
        public function 
    setAr  ( $key $value ){
            
    $this -> args [ $key ] =  $value ;
        }
    }

    class 
    ext2  extends  args2  {    
        public function 
    test  (){
            
    $this -> setAr ( 'k0' '1' );
            
    print_r ( $this -> args );
        }
    }
    $args  = new  args2 ();
    $test  = new  ext2 ();
    $test -> test ();
    print_r ( $args );
    in V1 wird das array $args in der instanz $args manipuliert, in v2 nicht.
    ist auch klar warum, danke murdoc

    // edit
    und hier mal ein grober nachbau von meinem beispiel.
    PHP:
    class  args  {
        public 
    $args  = array();
        public function 
    setAr  ( $key $value ){
            
    $this -> args [ $key ] =  $value ;
        }
    }

    class 
    EventHost {
        var 
    $eventListener ;
        
        public function 
    __construct (){
            
    $this -> setEventListener (new  DefaultListener ());
        }
        
        public function 
    setEventListener ( $pEventListener ){
            
    $this -> eventListener $pEventListener ;
        }
        
        public function 
    triggerEvent ( args $args ){
            foreach (
    $this -> eventListener -> event  as  $kEv  =>  $vEv ){
                if (
    is_callable (array( $this -> eventListener $vEv ))) {
                    
    print_r ( $this -> eventListener -> $vEv ( $args ));    
                }
                
            }
            
        }
    }

    class 
    DefaultListener  {
        
        public 
    $event  = array();
        
        public function 
    __construct (){
            
    $this -> setEvent ( 'onEvent' 'onEvent' );
        }
        
        public function 
    setEvent  ( $kEv $vEv ){
            
    $this -> event [ $kEv ] =  $vEv ;
        }
        
        public function 
    onEvent ( args $args ){
            
    $args -> setAr ( 'listener' get_class ());
            
    $args -> setAr ( 'event' $this -> event [ 'onEvent' ]);
            return 
    $args ;
        }
    }

    class 
    EventListener  extends  DefaultListener {
        
        public function 
    __construct (){
            
    $this -> setEvent ( 'onEvent' 'onEvent2' );
        }
        
        public function 
    onEvent2 ( args $args ){
            
    $args -> setAr ( 'listener' get_class ());
            
    $args -> setAr ( 'event' $this -> event [ 'onEvent' ]);
            return 
    $args ;
        }
    }

    class 
    EventListener2  extends  DefaultListener {
        public function 
    __construct (){
            
    $this -> setEvent ( 'onEvent' 'onLariVari' );
            
    $this -> setEvent ( 'onXXX' 'onXXX' );
        }
        public function 
    onLariVari ( args $args ){
            
    $args -> setAr ( 'listener' get_class ());
            
    $args -> setAr ( 'event' $this -> event [ 'onEvent' ]);
            return 
    $args ;
        }
        public function 
    onXXX ( args $args ){
            
    $args -> setAr ( 'listener' get_class ());
            
    $args -> setAr ( 'event' $this -> event [ 'onXXX' ]);
            return 
    $args ;
        }
    }

    $args  = new  args ();
    $eventhost = new  EventHost ();
    $eventhost -> triggerEvent ( $args );
    $eventhost -> setEventListener (new  EventListener );
    $eventhost -> triggerEvent ( $args );
    $eventhost -> setEventListener (new  EventListener2 );
    $eventhost -> triggerEvent ( $args );
    echo 
    "\n\rErgebnis von \$arg\n\r" ;
    print_r ( $args );
     
  8. 30. Mai 2012
    Zuletzt bearbeitet: 30. Mai 2012
    AW: frage bei bootstrap, event -> methoden aufbau

    hier vereinfacht ein beispiel:

    PHP:
    <? php

    class  Event
    {
      public 
    $target $name $data ;
      
      public function 
    __construct ( Target $target $name $data  null )
      {
        
    $this -> target  $target ;
        
    $this -> name    $name ;
        
    $this -> data    $data ;
      } 
    }

    // ----------------------

    class  Target
    {
      private 
    $_listeners  = [];
      
      public function 
    on ( $name , callable  $callback )
      {
        if (!isset(
    $this -> _listeners [ $name ]))
          
    $this -> _listeners [ $name ] = [];
          
        return 
    array_push ( $this -> _listeners [ $name ],  $callback ) -  1 ;
      }
      
      public function 
    off ( $name $id  null )
      {
        if (
    $id  ===  null ) {
          
    $this -> _listeners [ $name ] = [];
          return;
        } 
        
        unset(
    $this -> _listeners [ $name ][ $id ]);
      }
      
      public function 
    fire ( $name $data  null )
      {
        if (!isset(
    $this -> _listeners [ $name ])
         || (
    $c  count ( $this -> _listeners [ $name ])) ===  0 )
          return;
        
        
    $event  = new  Event ( $this $name $data );
        
        for (
    $i  0 $i  $c ; ++ $i ) {
          if (!isset(
    $this -> _listeners [ $name ][ $i ]))
            continue;
            
          
    call_user_func ( $this -> _listeners [ $name ][ $i ],  $event );
        }
      }


    // ----------------------

    class  Application
    {
      public function 
    __construct ()
      {
        
    $ctrl  = new  Controller ;
        
    $ctrl -> on ( 'afterdispatch' , [  $this 'onAfterDispatch'  ]);
        
    $ctrl -> dispatch ();
      }
      
      public function 
    onAfterDispatch ( Event $event )
      {
        print 
    'Class `'  get_class ( $event -> target ) .  '` fired "' 
          
    $event -> name  '" with data: '  $event -> data
      }
    }

    // ----------------------

    class  Controller  extends  Target
    {
      public function 
    dispatch ()
      {
        
    // jada jada jada
        
    $this -> fire ( 'afterdispatch' 'that is it, i am done!' );
      }
    }

    // ----------------------

    new  Application ;
    Code:
    Class `Controller` fired "afterdispatch" with data: that is it, i am done!
     
  9. 31. Mai 2012
    AW: frage bei bootstrap, event -> methoden aufbau

    danke, leider bekomm ich das nicht ans laufen.
    betrifft
    PHP:
    private  $_listeners  = [];
    wenn ich das mit array() anpasse kommt
    // php 5.3.1
     
  10. 31. Mai 2012
    Zuletzt bearbeitet: 31. Mai 2012
    AW: frage bei bootstrap, event -> methoden aufbau

    PHP 5.4
    PHP: Downloads

    für windows:
    PHP For Windows: Binaries and sources Releases

    extra syntax-zucker für PHP 5.4 wäre z.b. das:
    PHP:
    class  Application 
    {
      public function 
    __construct ()
      {
        
    $ctrl  = new  Controller ;
        
        
    $ctrl -> on ( 'afterdispatch'
          function(
    Event $event ) {
            
    // mach was z.b.:
            
    $this -> onAfterDispatch ( $event );
          }
        );
        
        
    $ctrl -> dispatch ( 'index' );
      }
    }
    PHP 5.3 version:
    Spoiler
    PHP:
    <? php

    class  Event
    {
      public 
    $target $name $data ;
      
      public function 
    __construct ( Target $target $name $data  null )
      {
        
    $this -> target  $target ;
        
    $this -> name    $name ;
        
    $this -> data    $data ;
      } 
    }

    // ----------------------

    class  Target
    {
      private 
    $_listeners  = array();
      
      public function 
    on ( $name $callback )
      {
        if (!
    is_callable ( $callback ))
          throw new 
    InvalidArgumentException ( '$callback must be callable' );
          
        if (!isset(
    $this -> _listeners [ $name ]))
          
    $this -> _listeners [ $name ] = array();
          
        return 
    array_push ( $this -> _listeners [ $name ],  $callback ) -  1 ;
      }
      
      public function 
    off ( $name $id  null )
      {
        if (
    $id  ===  null ) {
          
    $this -> _listeners [ $name ] = array();
          return;
        } 
        
        unset(
    $this -> _listeners [ $name ][ $id ]);
      }
      
      public function 
    fire ( $name $data  null )
      {
        if (!isset(
    $this -> _listeners [ $name ])
         || (
    $c  count ( $this -> _listeners [ $name ])) ===  0 )
          return;
        
        
    $event  = new  Event ( $this $name $data );
        
        for (
    $i  0 $i  $c ; ++ $i ) {
          if (!isset(
    $this -> _listeners [ $name ][ $i ]))
            continue;
            
          
    call_user_func ( $this -> _listeners [ $name ][ $i ],  $event );
        }
      }


    // ----------------------

    class  Application
    {
      public function 
    __construct ()
      {
        
    $ctrl  = new  Controller ;
        
    $ctrl -> on ( 'afterdispatch' , [  $this 'onAfterDispatch'  ]);
        
    $ctrl -> dispatch ();
      }
      
      public function 
    onAfterDispatch ( Event $event )
      {
        print 
    'Class `'  get_class ( $event -> target ) .  '` fired "' 
          
    $event -> name  '" with data: '  $event -> data
      }
    }

    // ----------------------

    class  Controller  extends  Target
    {
      public function 
    dispatch ()
      {
        
    // jada jada jada
        
    $this -> fire ( 'afterdispatch' 'that is it, i am done!' );
      }
    }

    // ----------------------

    new  Application
     
  11. 31. Mai 2012
    Zuletzt bearbeitet: 31. Mai 2012
    AW: frage bei bootstrap, event -> methoden aufbau

    vielen dank.
    // edit
    hier noch ne kleine erweiterung
    PHP:
    class  Event {
        public 
    $target $name $data ;
        
        public function 
    __construct ( Target $target $name $data  null ) {
            
    $this -> target  $target ;
            
    $this -> name    $name ;
            
    $this -> data    $data ;
        }
    }

    // ----------------------

    class  Target {
        private 
    $_listeners  = array();
        
        public function 
    on ( $name $callback $idx  null ){
            if (!isset(
    $this -> _listeners [ $name ]))
                
    $this -> _listeners [ $name ] = array();
            
            if (
    $idx  ===  null )
                return 
    array_push ( $this -> _listeners [ $name ],  $callback ) -  1 ;
            
            
    array_splice ( $this -> _listeners [ $name ],  $idx 0 , array( $callback ));
            return 
    $idx ;
        }
        
        public function 
    off ( $name $id  null ){
            if (
    $id  ===  null ) {
                
    $this -> _listeners [ $name ] = array();
                return;
            }
        
            unset(
    $this -> _listeners [ $name ][ $id ]);
        }
        
        public function 
    fire ( $name $data  null ){
            if (!isset(
    $this -> _listeners [ $name ]) || ( $c  count ( $this -> _listeners [ $name ])) ===  0 )
                return;
            
            
    $event  = new  Event ( $this $name $data );
            
            for (
    $i  0 $i  $c ; ++ $i ) {
                if (!isset(
    $this -> _listeners [ $name ][ $i ]))
                    continue;
                if (
    is_callable (array( $this -> _listeners [ $name ][ $i ][ 0 ],  $this -> _listeners [ $name ][ $i ][ 1 ]))) {
                    
    call_user_func ( $this -> _listeners [ $name ][ $i ],  $event );
                }
            }
        }
        
        public function 
    getListeners  (){
            return 
    $this -> _listeners ;
        }
    }

    // ----------------------

    class  Controller  extends  Target {
        public function 
    dispatch (){
            
    // jada jada jada
            
    foreach( $this -> getListeners () as  $kE  =>  $vE ){
                
    $this -> fire ( $kE 'that is it, i am done!' );
            }
        }
    }

    // ----------------------

    class  Application  {
        public function 
    __construct (){
            
    $ctrl  = new  Controller ;
            
    $ctrl -> on ( 'afterdispatch' , array( $this 'onAfterDispatch'  ), 1 );
            
    $ctrl -> on ( 'afterdispatch' , array( $this 'onAfterDispatch_'  ), 0 );
            
    $ctrl -> dispatch ();
        }
        
        public function 
    onAfterDispatch ( Event $event ){
            print 
    'Class `'  get_class ( $event -> target ) .  '` fired "'  $event -> name  '" with data: '  $event -> data
        }
        
        public function 
    onAfterDispatch_ ( Event $event ){
            print 
    '_Class `'  get_class ( $event -> target ) .  '` fired "'  $event -> name  '" with data: '  $event -> data
        }
    }

    // ----------------------

    new  Application ;
    - Target: n wurde ein index hinzugefügt um die reihenfolge zu beeinflussen
    - Target::fire eine prüfung is_callable() hinzugefügt
    - Target::getListeners hinzugefügt
    - Controller::dispatch wurde eine schleife hinzugefügt

    so, jetzt mal gucken was man damit anstellen kann.
    nochmals vielen dank und @Nanobyte BW kommt noch.

    lassen den beitrag noch was offen.
     
  12. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.