SitePoint Sponsor

User Tag List

Page 2 of 3 FirstFirst 123 LastLast
Results 26 to 50 of 55
  1. #26
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by blueyon View Post
    I think the & is still required for this part of the code.

    I have checked the PHP5 documentation and as far as I know refrences are only pasted by default when returning a object from a method or function.

    Can you please give me details of where to use references?
    You do not need to use references in PHP5. At all.

  2. #27
    SitePoint Addict
    Join Date
    Feb 2006
    Posts
    281
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ok Cool!

  3. #28
    SitePoint Addict
    Join Date
    Jan 2007
    Posts
    323
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    You do not need to use references in PHP5. At all.
    Not even for passing variables to functions?

  4. #29
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    Well since you what a more complete Registry... (not perfect, far from it.) Handles more then just objects of course.
    This one is good

  5. #30
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by atDev View Post
    Not even for passing variables to functions?
    When you pass other things than objects, you still need to use &.

  6. #31
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by wysiwyg View Post
    When you pass other things than objects, you still need to use &.
    Assuming that you need to pass by reference, which is hardly ever a good idea with plain values.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  7. #32
    SitePoint Evangelist
    Join Date
    Mar 2006
    Location
    Sweden
    Posts
    451
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, when dealing with sessions, it can be quite handy to pass that by reference. But other than that, I agree with you.

  8. #33
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Finland
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    IMO dynamic registry is bad way to implement it, eventually you can't remember what things you have in your registry anymore because you can't use standard documentation syntax. It's not so hard to write registry class with properly introduced member variables.

    PHP Code:
    class Registry{
        
    /**
        * @var DB_Connection
        */  
       
    public $dbConnection null;
       
      
    /**
      * @var Mailer
      */
       
    public $mailer null;
     ....
      
    __set($name,$object){
         if (isset(
    $this->$name)){
             throw new 
    Registry_Exception('Registy::$'.$name.' already set!');  
         }
         
    $this->$name $object;
       }

    Now it's hundred times easier for anyone to see whats in the box and if you use advanced IDE like PDT you will have automatic code completion.

  9. #34
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JaskaS View Post
    IMO dynamic registry is bad way to implement it, eventually you can't remember what things you have in your registry anymore because you can't use standard documentation syntax. It's not so hard to write registry class with properly introduced member variables.

    ...snip...

    Now it's hundred times easier for anyone to see whats in the box and if you use advanced IDE like PDT you will have automatic code completion.
    If you want that then all you need to do is either extend the dynamic Registry and add your helper functions that isn't so hard either is it? But your method you lose the ability to create multiple database connections (or multiple of anything) and store those in the registry.

    Your method is far to inflexible in my mind.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  10. #35
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Finland
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    If you want that then all you need to do is either extend the dynamic Registry and add your helper functions that isn't so hard either is it? But your method you lose the ability to create multiple database connections (or multiple of anything) and store those in the registry.
    No you don't

    Just add new member variable

    PHP Code:
    class Registry{
      ....
       
    /**
       * This is database connection to server2
       * @var DB_Connection
       */
       
    public $dbConnectionToServer2 null   


    Or you could store connection container to registry

    PHP Code:
     class Registry{
      ....
       
    /**
       * @var DB_Connections
       */
       
    public $dbConnections null   


    $user_connection = $registry->dbConnections->get(DB_Connections::USER);
    $admin_connection = $registry->dbConnections->get(DB_Connections::ADMIN);

  11. #36
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    You have to manually edit the class then to add support for multiple instance. That is an inflexible solution.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  12. #37
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Finland
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    You have to manually edit the class then to add support for multiple instance. That is an inflexible solution.
    Well registry is solid (not dynamic) container. All objects it contains should always be available so it makes sense to introduce them properly. I have used dynamic registry for long time by myself and just recently realized that solid registry is much clearer/better. But that's just my opinion.

  13. #38
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JaskaS View Post
    ...All objects it contains should always be available so it makes sense to introduce them properly...
    They are only available if you set them which is the same for a dynamic registry. And if you are setting them inside the Registry __construct then you are doing something far worse.

    PHP Code:
    $reg solidRegistry();
    $reg->database = new Database();
    $reg->mail = new Mail(); 
    PHP Code:
    $reg dynRegistry();
    $reg->add'database', new Database() );
    $reg->add'mail', new Mail() );

    // With magic methods can do:
    $reg->database = new Database();
    $reg->mail = new Mail(); 
    It is exactly the same the only difference is the dynamic Registry can me extended at run time theres no need to rewrite the object just to add a new variable.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  14. #39
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Finland
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    They are only available if you set them which is the same for a dynamic registry. And if you are setting them inside the Registry __construct then you are doing something far worse.
    Of course the only benefit you get by using solid registry compared to dynamic is that you can easily document your code, which is really important in bigger projects (more than one programmer) and helpful when using PDT because of code completion. I just hate it that I have to find and open the script where the registry is initialized if I can't remember which identifier I used for databaseConnection123 when I stored it in the registry 2 months ago. If I have documented member variables or methods in registry class I just have to press ctrl + space to find that out.

  15. #40
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    I still don't see a point in using a solid registry if all you want is auto complete then it can be archived simply by extending the class with your own methods or properties.

    PHP Code:
    class ccRegistry_Database extends ccRegistry
    {
        public function 
    databaseOne $object null )
        {

            if ( empty( 
    $object ) ) {
                return 
    $this->call'databaseone' );
            }

            return 
    $this->add'databaseone'$object );

        }

        public function 
    databaseTwo $object null )
        {

            if ( empty( 
    $object ) ) {
                return 
    $this->call'databasetwo' );
            }

            return 
    $this->add'databasetwo'$object );

        }
    }

    $d = new ccRegistry_Database();
    $d->databaseOne();
    $d->add'databasethree', new Database() );
    $d->add'var''value' ); 
    And it still has the ability to be dynamic. There a hybrid best of both worlds.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  16. #41
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Finland
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    But registry isn't dynamic by definition. It can't be because you should be able to get any object from registry at any time and they must be found by certain identifier, so you can't alter your registry on the fly. Objects using registry must be able to rely on that the registry has always the same interface. So I don't see the point in registry being dynamic

  17. #42
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do not extend the registry if it has static variables inside.

  18. #43
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by JaskaS View Post
    But registry isn't dynamic by definition. It can't be because you should be able to get any object from registry at any time and they must be found by certain identifier, so you can't alter your registry on the fly. Objects using registry must be able to rely on that the registry has always the same interface. So I don't see the point in registry being dynamic
    You seem to be confused what a Registry is. It is a controlled global store of data.

    The Registry provides a mechanism for storing data globally in a well managed fashion, helping to prevent global meltdown.
    A Registry needs to be dynamic to be a global store.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  19. #44
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    You seem to be confused what a Registry is. It is a controlled global store of data. A Registry needs to be dynamic to be a global store.
    True.

  20. #45
    SitePoint Enthusiast
    Join Date
    Jul 2004
    Location
    Finland
    Posts
    73
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well maybe I am, but I use registry as a global access to common objects and services (to replace singletons), not as a global storage where you can throw anything you want, that sounds like a registry would become a mess in no time.

  21. #46
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    You do not need to use references in PHP5. At all.
    What about output parameters in functions?

  22. #47
    SitePoint Wizard silver trophy kyberfabrikken's Avatar
    Join Date
    Jun 2004
    Location
    Copenhagen, Denmark
    Posts
    6,157
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mastodont View Post
    What about output parameters in functions?
    There's pretty much a consensus on the view, that passing parameters by reference, is a bad idea. Functions shouldn't modify their input parameters; Any output, should be returned.

    There are a couple of built-in functions in the core PHP language, which use parameters by reference. I would say, that they are crafted in that way, because they were written by C programmers. In C, there are technical arguments for passing by reference (Or at least the semantical equivalent concept in C). These do not pertain to PHP.

  23. #48
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    The only time I see where it is required/usable to use references where the function alters the original data is in the case of array_pop & array_shift.

    I see no other way to do it, take the first/last item from the array return it and still have it gone from the array. Well there is probably a way I just haven't figured out yet.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  24. #49
    ¬.¬ shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    For the curious my current Registry implementation:
    Only the interface is static the actual part the stores the values is an object.

    PHP Code:
    class ccRegistry
    {
        static protected 
    $_instance  null;
        static protected 
    $_classname 'ccRegistryStore';

        
    #---------------------------------------------------------------------------
        # Instance Managers

        
    static function setInstance ccRegistryStore $init )
        {
            if ( !empty( 
    self::$_instance ) ) {

                require_once 
    'CCentral/Exception/Registry.php';
                throw new 
    ccRegistryException'Registry is already intilized.' );
                return 
    false;

            }

            
    self::setClassnameget_class$init ) );
            
    self::$_instance $init;
        }

        static function 
    getInstance ()
        {
            if ( empty( 
    self::$_instance ) ) {
                
    self::setInstance( new self::$_classname );
            }

            return 
    self::$_instance;
        }

        static function 
    endInstance ()
        {
            unset( 
    self::$_instance );
        }

        
    #---------------------------------------------------------------------------

        // @TODO: Finish, while making it more robust.
        
    static function setClassname $class null )
        {
            if ( empty( 
    $class ) ) {

                
    self::$_classname 'ccRegistryStore';
                require_once 
    'CCentral/Registry/Store.php';
                return;

            }

            
    $path str_replace'_''/'$class );
            if ( 
    substr$path0) === 'cc' ) {
                
    $path 'CCentral/' substr$class);
            }

            if ( 
    class_exists$class ) ) {
                
    self::$_classname $class;
            }
        }

        
    #---------------------------------------------------------------------------

        
    static function exists $name )
        {
            return 
    self::$_instance->exists$name );
        }

        static function 
    add $name$value null$overwrite false )
        {
            
    self::$_instance->add$name$value$overwrite );
        }

        static function 
    call $name )
        {
            return 
    self::$_instance->call$name );
        }

        static function 
    delete $name )
        {
            
    self::$_instance->delete$name );
        }

    PHP Code:
    class ccRegistryStore extends ArrayObject
    {
        public function 
    offsetSet $name$value$overwrite false )
        {
            
    $key $this->_clean_name$name );

            if ( isset( 
    $this$key ] ) && !$overwrite ) {

                require_once 
    'CCentral/Exception/Registry.php';
                throw new 
    ccRegistryException'"%s" is already defined. Cannot overwrite without permission.'$name );
                return 
    false;

            }

            
    parent::offsetSet$key$value );
            return 
    $this;
        }

        public function 
    offsetGet $name )
        {
            
    $key $this->_clean_name$name );

            if ( isset( 
    $this$key ] ) ) {
                return 
    parent::offsetGet$key );
            }

            require_once 
    'CCentral/Exception/Registry.php';
            throw new 
    ccRegistryException'"%s" is undefined in the registry'$name );
            return 
    false;
        }

        public function 
    offsetUnset $name )
        {
            
    $key $this->_clean_name$name );

            if ( isset( 
    $this$key ] ) ) {
                
    parent::offsetUnset$key );
                return 
    $this;
            }

            require_once 
    'CCentral/Exception/Registry.php';
            throw new 
    ccRegistryException'"%s" is undefined in the registry'$name );
            return 
    false;
        }

        
    #---------------------------------------------------------------------------
        # Function Aliases.

        
    public function add $name$value null$overwrite false )
        {
            return 
    $this->offsetSet$name$value$overwrite );
        }

        public function 
    overrwrite $name$value null )
        {
            return 
    $this->offsetSet$name$valuetrue );
        }

        public function 
    call $name )
        {
            return 
    $this->offsetGet$name );
        }

        public function 
    delete $name )
        {
            return 
    $this->offsetUnset$name );
        }

        
    #---------------------------------------------------------------------------

        
    public function offsetExists $name )
        {
            return 
    array_key_exists$name$this );
        }

        public function 
    exists $name )
        {
            
    $this->offsetExists$name );
        }

        
    #---------------------------------------------------------------------------

        
    protected function _clean_name $name )
        {
            return 
    preg_replace'/[^a-z0-9_]+/'''strtolowertrim$name ) ) );
        }

    PHP Code:
    $r ccRegistry::getInstance();
    $r['database'] = new Database();
    $r['mail'] = new Mail(); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  25. #50
    SitePoint Addict Mastodont's Avatar
    Join Date
    Mar 2007
    Location
    Czech Republic
    Posts
    375
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken View Post
    Functions shouldn't modify their input parameters
    No doubt about it, but I talked about output parameters. If I need something with more return values, then C style is the best. For example parsing URI:

    PHP Code:
    $success parse($actualurl$com$action$lang, ...);

    ...

    function 
    parse($url, &$component, &action, &language, ...) 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •