Pagina 1 di 6 1 2 3 ... ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 51
  1. #1

    Metodo php che richiama al proprio interno lo stesso metodo ovvero metodo incomprensibile... HELP PLEASE!

    C'è un programma che non riesco a capire come funziona. Lo script è costituito da decine di file .php di centinaia di righe con namespace diversi e nel leggerlo si salta da un file ad un altro in continuazione. Il pezzo che non capisco è questo:
    Codice PHP:
    namespace woo\domain;
    class 
    Venue extends DomainObject {
    ...
            static function 
    findAll() {
                   
    $finder self::getFinder__CLASS__ ); 
            return 
    $finder->findAll();
            }
    ... 
    findAll chiama il metodo getFinder statico di DomainObject e lo salva in $finder poi cosa fa?
    Codice PHP:
    return $finder->findAll(); 
    Più pratica in futuro...

  2. #2
    ...e secondo te è superfluo postare il codice del metodo getFinder()?
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  3. #3
    Quote Originariamente inviata da satifal Visualizza il messaggio
    ...e secondo te è superfluo postare il codice del metodo getFinder()?
    Codice PHP:
    static function getFinder$type ) {
            return 
    HelperFactory::getFinder$type ); 
        } 
    ma non credo basti... metto tutte le classi così è la volta buona che mi maledite!
    Ultima modifica di giannino1995; 06-10-2014 a 17:27
    Più pratica in futuro...

  4. #4
    listing12.08.php
    Codice PHP:
    <?php
    require_once("woo/domain/Venue.php");
    try {
        
    $venues = \woo\domain\Venue::findAll();
        print 
    "<br><br>";
        
    print_r($venues);
        print 
    "<br><br>";
    } catch ( 
    Exception $e ) {
        include( 
    'error.php' );
        exit(
    0);
    }

    // default page follows
    ?>
    <html>
    <head>
    <title>Venues</title>
    </head>
    <body>
    <h1>Venues</h1>

    <?php foreach( $venues as $venue ) { ?>
        <?php print $venue->getName(); ?><br />
    <?php ?>

    </body>
    </html>
    Venue.php
    Codice PHP:
    <?php
    namespace woo\domain;

    require_once( 
    "woo/domain/DomainObject.php" );

    class 
    Venue extends DomainObject {
        private 
    $name;
        private 
    $spaces;

        function 
    __construct$id=null$name=null ) {
            
    $this->name $name;
            
    parent::__construct$id );
        }
        
        function 
    setSpacesSpaceCollection $spaces ) {
            
    $this->spaces $spaces;
        } 

        function 
    getSpaces() {
            if ( ! isset( 
    $this->spaces ) ) {
                
    $finder self::getFinder'woo\\domain\\Space' ); 
                
    $this->spaces $finder->findByVenue$this->getId() );
                
    //$this->spaces = self::getCollection("woo\\domain\\Space");
            
    }
            return 
    $this->spaces;
        } 

        function 
    addSpaceSpace $space ) {
            
    $this->getSpaces()->add$space );
            
    $space->setVenue$this );
        }

        function 
    setName$name_s ) {
            
    $this->name $name_s;
            
    $this->markDirty();
        }

        function 
    getName( ) {
            return 
    $this->name;
        }
        
        static function 
    findAll() {
            
    $finder self::getFinder__CLASS__ ); 
            return 
    $finder->findAll();
        }
        static function 
    find$id ) {
            
    $finder self::getFinder__CLASS__ ); 
            return 
    $finder->find$id );
        }

    }

    ?>
    DomainObject.php
    Codice PHP:
    <?php
    namespace woo\domain;

    require_once( 
    "woo/domain/Collections.php" );
    require_once( 
    "woo/domain/ObjectWatcher.php" );
    require_once( 
    "woo/domain/HelperFactory.php" );

    abstract class 
    DomainObject {
        private 
    $id = -1;

        function 
    __construct$id=null ) {
            if ( 
    is_null$id ) ) {
                
    $this->markNew();
            } else {
                
    $this->id $id;
            }
        }

        function 
    markNew() {
            
    ObjectWatcher::addNew$this );
        }

        function 
    markDeleted() {
            
    ObjectWatcher::addDelete$this );
        }

        function 
    markDirty() {
            
    ObjectWatcher::addDirty$this );
        }

        function 
    markClean() {
            
    ObjectWatcher::addClean$this );
        }


        function 
    getId( ) {
            return 
    $this->id;
        }

        static function 
    getCollection$type ) {
            return 
    HelperFactory::getCollection$type ); 
        }
     
        function 
    collection() {
            return 
    self::getCollectionget_class$this ) );
        }

        function 
    finder() {
            return 
    self::getFinderget_class$this ) );
        }

        static function 
    getFinder$type ) {
            return 
    HelperFactory::getFinder$type ); 
        }

        function 
    setId$id ) {
            
    $this->id $id;
        }

        function 
    __clone() {
            
    $this->id = -1;
        }
    }
    ?>
    HelpFactory.php
    Codice PHP:
    <?php
    namespace woo\domain;

    if ( ! isset( 
    $EG_DISABLE_INCLUDES ) ) {
        require_once( 
    "woo/mapper/VenueMapper.php" );
        require_once( 
    "woo/mapper/SpaceMapper.php" );
        require_once( 
    "woo/mapper/EventMapper.php" );
        require_once( 
    "woo/mapper/Collections.php" );
    }

    class 
    HelperFactory {
        static function 
    getFinder$type ) {
            print 
    $type."<br>";
            
    $type preg_replace'|^.*\\\|'""$type );
            print 
    $type."<br>";
            
    $mapper "\\woo\\mapper\\{$type}Mapper";
            if ( 
    class_exists$mapper ) ) {
                return new 
    $mapper();
            }
            throw new \
    woo\base\AppException"Unknown: $mapper);
        }

        static function 
    getCollection$type ) {
            
    $type preg_replace'|^.*\\\|'""$type );
            
    $collection "\\woo\\mapper\\{$type}Collection";
            if ( 
    class_exists$collection ) ) {
                return new 
    $collection();
            }
            throw new \
    woo\base\AppException"Unknown: $collection);
        }
    }
    ?>
    VenueMapper.php
    Codice PHP:
    <?php
    namespace woo\mapper;


    require_once( 
    "woo/mapper/Mapper.php" );
    require_once( 
    "woo/base/Exceptions.php" );
    require_once( 
    "woo/mapper/Collections.php" );
    require_once( 
    "woo/domain.php" );

    class 
    VenueMapper extends Mapper 
                                 
    implements \woo\domain\VenueFinder {

        function 
    __construct() {
            
    parent::__construct();
            
    $this->selectAllStmt self::$PDO->prepare
                                
    "SELECT * FROM venue");
            
    $this->selectStmt self::$PDO->prepare
                                
    "SELECT * FROM venue WHERE id=?");
            
    $this->updateStmt self::$PDO->prepare
                                
    "UPDATE venue SET name=?, id=? WHERE id=?");
            
    $this->insertStmt self::$PDO->prepare
                                
    "INSERT into venue ( name ) 
                                 values( ? )"
    );
        } 
        
        function 
    getCollection( array $raw ) {
            return new 
    SpaceCollection$raw$this );
        }

        protected function 
    doCreateObject( array $array ) {
            
    $obj = new \woo\domain\Venue$array['id'] );
            
    $obj->setname$array['name'] );
            
    //$space_mapper = new SpaceMapper();
            //$space_collection = $space_mapper->findByVenue( $array['id'] );
            //$obj->setSpaces( $space_collection );
            
    return $obj;
        }

        protected function 
    targetClass() {
            return 
    "woo\\domain\\Venue";
        }

        protected function 
    doInsert( \woo\domain\DomainObject $object ) {
            
    $values = array( $object->getname() ); 
            
    $this->insertStmt->execute$values );
            
    $id self::$PDO->lastInsertId();
            
    $object->setId$id );
        }
        
        function 
    update( \woo\domain\DomainObject $object ) {
            
    $values = array( $object->getname(), $object->getid(), $object->getId() ); 
            
    $this->updateStmt->execute$values );
        }

        function 
    selectStmt() {
            return 
    $this->selectStmt;
        }

        function 
    selectAllStmt() {
            return 
    $this->selectAllStmt;
        }

    }
    ?>
    Mapper.php
    Codice PHP:
    <?php
    namespace woo\mapper;


    require_once(
    "woo/base/Registry.php");
    require_once(
    "woo/base/Exceptions.php");
    require_once(
    "woo/domain/Finders.php");

    abstract class 
    Mapper implements \woo\domain\Finder {
        protected static 
    $PDO
        function 
    __construct() {
     
            if ( ! isset(
    self::$PDO) ) { 
                
    $dsn = \woo\base\ApplicationRegistry::getDSN( );
                
    print_r$dsn );
                print 
    "<br>".$dsn;
                if ( 
    is_null$dsn ) ) {
                    throw new \
    woo\base\AppException"No DSN" );
                }
                
    self::$PDO = new \PDO$dsn );
                
    self::$PDO->setAttribute(\PDO::ATTR_ERRMODE, \PDO::ERRMODE_EXCEPTION);
            }
        }

        private function 
    getFromMap$id ) {
            return \
    woo\domain\ObjectWatcher::exists
                    
    $this->targetClass(), $id );
        }

        private function 
    addToMap( \woo\domain\DomainObject $obj ) {
            return \
    woo\domain\ObjectWatcher::add$obj );
        }

        function 
    find$id ) {
            
    $old $this->getFromMap$id );
            if ( 
    $old ) { return $old; }
            
    $this->selectstmt()->execute( array( $id ) );
            
    $array $this->selectstmt()->fetch( ); 
            
    $this->selectstmt()->closeCursor( );
            if ( ! 
    is_array$array ) ) { return null; }
            if ( ! isset( 
    $array['id'] ) ) { return null; }
            
    $object $this->createObject$array );
            
    $object->markClean();
            return 
    $object
        }

        function 
    findAll( ) {
            
    $this->selectAllStmt()->execute( array() );
            return 
    $this->getCollection$this->selectAllStmt()->fetchAll( \PDO::FETCH_ASSOC ) );
        }
     
        function 
    createObject$array ) {
            
    $old $this->getFromMap$array['id']);
            if ( 
    $old ) { return $old; }
            
    $obj $this->doCreateObject$array );
            
    $this->addToMap$obj );
            
    //$obj->markClean();
            
    return $obj;
        }

        function 
    insert( \woo\domain\DomainObject $obj ) {
            
    $this->doInsert$obj );
            
    $this->addToMap$obj );
            
    $obj->markClean();
        }

    //  abstract function update( \woo\domain\DomainObject $object );
        
    protected abstract function getCollection( array $raw );
        protected abstract function 
    doCreateObject( array $array );
        protected abstract function 
    doInsert( \woo\domain\DomainObject $object );
        protected abstract function 
    targetClass();
        protected abstract function 
    selectStmt( );
        protected abstract function 
    selectAllStmt( );
    }
    ?>
    Più pratica in futuro...

  5. #5
    Registry.php
    Codice PHP:
    <?php
    namespace woo\base;
    require_once(
    "woo/controller/Request.php");

    abstract class 
    Registry {
        abstract protected function 
    get$key );
        abstract protected function 
    set$key$val );
    }

    class 
    RequestRegistry extends Registry {
        private 
    $values = array();
        private static 
    $instance=null;

        private function 
    __construct() {}

        static function 
    instance() {
            if ( 
    is_null(self::$instance) ) { self::$instance = new self(); }
            return 
    self::$instance;
        }

        protected function 
    get$key ) {
            if ( isset( 
    $this->values[$key] ) ) {
                return 
    $this->values[$key];
            }
            return 
    null;
        }

        protected function 
    set$key$val ) {
            
    $this->values[$key] = $val;
        }

        static function 
    getRequest() {
            
    $inst self::instance();
            if ( 
    is_null$inst->get"request" ) ) ) {
                
    $inst->set('request', new \woo\controller\Request() );
            }
            return 
    $inst->get"request" );
        }

    }

    class 
    SessionRegistry extends Registry {
        private static 
    $instance=null;
        private function 
    __construct() {
            
    session_start();
        }

        static function 
    instance() {
            if ( 
    is_null(self::$instance) ) { self::$instance = new self(); }
            return 
    self::$instance;
        }

        protected function 
    get$key ) {
            if ( isset( 
    $_SESSION[__CLASS__][$key] ) ) {
                return 
    $_SESSION[__CLASS__][$key];
            }
            return 
    null;
        }

        protected function 
    set$key$val ) {
            
    $_SESSION[__CLASS__][$key] = $val;
        }

        function 
    setDSN$dsn ) {
            
    self::instance()->set('dsn'$dsn );
        }

        function 
    getDSN( ) {
            return 
    self::instance()->get("dsn");
        }
    }

        class 
    ApplicationRegistry extends Registry {
        private static 
    $instance=null;
        private 
    $freezedir "data";
        private 
    $values = array();
        private 
    $mtimes = array();

        private 
    $request=null;

        private function 
    __construct() { }

        static function 
    clean() {
            
    self::$instance=null;
        }

        static function 
    instance() {
            if ( 
    is_null(self::$instance) ) { self::$instance = new self(); }
            return 
    self::$instance;
        }

        protected function 
    get$key ) {
            
    $path $this->freezedir DIRECTORY_SEPARATOR $key;
            if ( 
    file_exists$path ) ) {
                
    clearstatcache();
                
    $mtime=filemtime$path );
                print 
    "$mtime"."<br>";
                if ( ! isset(
    $this->mtimes[$key] ) ) { $this->mtimes[$key]=0; }
                if ( 
    $mtime $this->mtimes[$key] ) {
                    
    $data file_get_contents$path );
                    print 
    "data: ".$data."<br>";
                    
    $this->mtimes[$key]=$mtime;
                    
    $this->values[$key]=unserialize$data );
                    print 
    "values: ".$this->values[$key]."<br>";
                    return 
    $this->values[$key];
                    
                }
            }
            if ( isset( 
    $this->values[$key] ) ) {
                return 
    $this->values[$key];
            }
            return 
    null;
        }

        protected function 
    set$key$val ) {
            
    $this->values[$key] = $val;
            
    $path $this->freezedir DIRECTORY_SEPARATOR $key;
            
    file_put_contents$pathserialize$val ) );
            
    $this->mtimes[$key]=time();
        }

        static function 
    getDSN() {
            return 
    self::instance()->get('dsn');
        }

        static function 
    setDSN$dsn ) {
            return 
    self::instance()->set('dsn'$dsn);
        }

        static function 
    setControllerMap( \woo\controller\ControllerMap $map  ) {
            
    self::instance()->set'cmap'$map );
        }

        static function 
    getControllerMap() {
            return 
    self::instance()->get'cmap' );
        }

        static function 
    appController() {
            
    $obj self::instance();
            if ( ! isset( 
    $obj->appController ) ) {
                
    $cmap $obj->getControllerMap();
                
    $obj->appController = new \woo\controller\AppController$cmap );
            }
            return 
    $obj->appController;
        }

        static function 
    getRequest() {
            
    $inst self::instance();
            if ( 
    is_null$inst->request ) ) {
                
    $inst->request = new \woo\controller\Request();
            }
            return 
    $inst->request;
        }
    }

    class 
    MemApplicationRegistry extends Registry {
        private static 
    $instance=null;
        private 
    $values=array();
        private 
    $id;

        private function 
    __construct() { }

        static function 
    instance() {
            if ( 
    is_null(self::$instance) ) { self::$instance = new self(); }
            return 
    self::$instance;
        }

        protected function 
    get$key ) {
            return \
    apc_fetch$key );
        }

        protected function 
    set$key$val ) {
            return \
    apc_store$key$val );
        }

        static function 
    getDSN() {
            return 
    self::instance()->get("dsn");
        }

        static function 
    setDSN$dsn ) {
            return 
    self::instance()->set("dsn"$dsn);
        }

    }

    ?>
    Finders.php
    Codice PHP:
    <?php
    namespace woo\domain;


    interface 
    Finder {
        function 
    find$id );
        function 
    findAll();

        function 
    updateDomainObject $object );
        function 
    insertDomainObject $obj );
        
    //function delete();
    }

    interface 
    SpaceFinder extends Finder {
        function 
    findByVenue$id );
    }

    interface 
    VenueFinder  extends Finder {
    }

    interface 
    EventFinder  extends Finder {
    }
    ?>
    Più pratica in futuro...

  6. #6
    Ovviamente c'è tanto codice inutile che non viene usato... si tratta di andare a cercare il metodo giusto richiamato da un altro metodo e di saltare da un file all'altro...
    Più pratica in futuro...

  7. #7
    $finder è un'istanza del corrispondente Mapper (nel caso in questione VenueMapper) il quale estende Mapper. Per cui il metodo findAll() invocato è quello implementato nella classe Mapper.


    Quote Originariamente inviata da giannino1995 Visualizza il messaggio
    Ovviamente c'è tanto codice inutile che non viene usato...
    ...secondo stai sbagliando approccio!
    "Mai discutere con un idiota. Ti trascina al suo livello e ti batte con l'esperienza." (Oscar Wilde)

  8. #8
    Ho rivisto il codice e sono arrivato a capire come il programma ottiene $venues ma non vedo getName() in woo\mapper\SpaceCollection Object.
    Codice PHP:
    class SpaceCollection 
            
    extends Collection
            
    implements \woo\domain\SpaceCollection {

        function 
    targetClass( ) {
            return 
    "\woo\domain\Space";
        }

    In Collection non c'è e in SpaceCollection neppure. Collection estende \Iterator ma quest'ultima classe non contiene getName o perlomeno non mi risulta. L'array che contiene i dati di $venues è salvato in una variabile $raw protetta di Collection...
    Qui sotto la stampa di $venues:
    codice:
    woo\mapper\SpaceCollection Object (     [mapper:protected] => woo\mapper\VenueMapper Object         (             [selectAllStmt] => PDOStatement Object                 (                     [queryString] => SELECT * FROM venue                 )              [selectStmt] => PDOStatement Object                 (                     [queryString] => SELECT * FROM venue WHERE id=?                 )              [updateStmt] => PDOStatement Object                 (                     [queryString] => UPDATE venue SET name=?, id=? WHERE id=?                 )              [insertStmt] => PDOStatement Object                 (                     [queryString] => INSERT into venue ( name )                               values( ? )                 )          )      [total:protected] => 5     [raw:protected] => Array         (             [0] => Array                 (                     [id] => 1                     [name] => bob                 )              [1] => Array                 (                     [id] => 2                     [name] => The Eyeball Inn                 )              [2] => Array                 (                     [id] => 3                     [name] => The Eyeball Inn                 )              [3] => Array                 (                     [id] => 4                     [name] => The Eyeball Inn                 )              [4] => Array                 (                     [id] => 5                     [name] => The Eyeball Inn                 )          )      [result:woo\mapper\Collection:private] =>      [pointer:woo\mapper\Collection:private] => 0     [objects:woo\mapper\Collection:private] => Array         (         )  )
    Qui invece il metodo che non riesco a trovare da nessuna parte:
    Codice PHP:
    <?php foreach( $venues as $venue ) { ?>
        <?php print $venue->getName(); ?><br />
    <?php ?>
    Qui trovi i codici:
    http://www.apress.com/9781430260318
    Il file che sto vedendo è questo:
    9781430260318_Chapter_12_Code/listing12.08.php
    Più pratica in futuro...

  9. #9
    Questa cosa mi sta facendo andare fuori di testa...
    Ultima modifica di giannino1995; 08-10-2014 a 10:37
    Più pratica in futuro...

  10. #10
    Quote Originariamente inviata da satifal Visualizza il messaggio
    $finder è un'istanza del corrispondente Mapper (nel caso in questione VenueMapper) il quale estende Mapper. Per cui il metodo findAll() invocato è quello implementato nella classe Mapper.




    ...secondo stai sbagliando approccio!
    Dove sei finito satifal?
    Più pratica in futuro...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2026 vBulletin Solutions, Inc. All rights reserved.