SitePoint Sponsor

User Tag List

Page 3 of 8 FirstFirst 1234567 ... LastLast
Results 51 to 75 of 190
  1. #51
    SitePoint Guru Galo's Avatar
    Join Date
    May 2005
    Location
    Holland!
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This code was posted by ezku, it's shows how clean php coden can be

    Quote Originally Posted by Ezku
    Here's something experimental I threw together a year ago. Oh, how this makes me wish PHP had closures...
    PHP Code:
    /**
     * Delegate interface. Describes a kind of refined callback.
     * 
     * @author        Ezku (dmnEe0@gmail.com)
     * @since        Sep 6, 2005
     */
    interface IDelegate
    {
        
    /**
         * Invoke Delegate
         * @param    array    arguments, optional
         */
        
    public function invoke($args = array());
    }

    /**
     * Calls a method on an object upon invocation.
     * 
     * @author        Ezku (dmnEe0@gmail.com)
     * @since        Sep 6, 2005
     */
    class Delegate implements IDelegate
    {
        private 
    $subordinate NULL;
        private 
    $method NULL;
        
        public function 
    __construct($subordinate$method)
        {
            
    $this->subordinate $subordinate;
            
    $this->method $method;
        }
        
        public function 
    invoke($args = array())
        {
            
    $this->subordinate Handle::resolve($this->subordinate);
            
    $callback = array(&$this->subordinate$this->method);
            return 
    call_user_func_array($callback$args);
        }

    PHP Code:
    /**
     * "A Handle represents an uninstantiated object that takes the place of a
     * given object and can be swapped out for the object.
     * Implements lazy loading for composing object heirarchies."
     * 
     * @author        Ezku (dmnEe0@gmail.com)
     * @since        Jul 12, 2005
     * @see            [url]http://wact.sourceforge.net/index.php/ResolveHandle[/url]
     */
    class Handle
    {
        
    /**
         * @var    string    class name
         */
        
    protected $class NULL;
        
    /**
         * @var    array    class constructor arguments
         */
        
    protected $args = array();
        
        
    /**
         * @param    string    class name
         * @param    array    class constructor arguments, optional
         */
        
    public function __construct($class$args = array())
        {
            
    $this->class    = (string)    $class;
            
    $this->args        = (array)    $args;
        }
        
        
    /**
         * Resolves a Handle; replaces a Handle instance with its identified class
         * @param    object    Handle
         * @return    object
         */
        
    static public function resolve($handle)
        {
            if (
    $handle instanceof self)
            {
                
    $handle call_user_constructor_array($handle->getClass(), $handle->getArgs());
            }
            return 
    $handle;
        }
        
        public function 
    getClass() { return $this->class; }
        public function 
    getArgs() { return $this->args; }

    PHP Code:
    /**
     * Simple Event: attach a Delegate and trigger.
     * @author        Ezku (dmnEe0@gmail.com)
     * @since        1.10.2005
     */
    class Event
    {
        private 
    $listener NULL;
        
        
    /**
         * @param    object    IDelegate
         * @return    boolean    overridden
         */
        
    public function attach(IDelegate $listener)
        {
            
    $overridden = isset($this->listener);
            
    $this->listener $listener;
            return 
    $overridden;
        }
        
        
    /**
         * @param    array    invocation arguments
         * @return    mixed    results
         */
        
    public function trigger($args = array())
        {
            return 
    $this->listener->invoke($args);
        }
        
        
    /**
         * Get attached listener
         * @return    object    IDelegate
         */
        
    public function getAttached()
        {
            return 
    $this->listener;
        }
    }
    /**
     * Multicast Event: attach multiple Delegates.
     * @author        Ezku (dmnEe0@gmail.com)
     * @since        1.10.2005
     */
    class MulticastEvent extends Event
    {
        private 
    $listeners = array();
        
        
    /**
         * @param    object    IDelegate
         */
        
    public function attach(IDelegate $delegate)
        {
            
    $this->listeners[] = $delegate;
        }
        
        
    /**
         * @param    array    invocation arguments
         * @return    array    results
         */
        
    public function trigger($args = array())
        {
            
    $return = array();
            foreach(
    $this->listeners as $listener)
            {
                
    $return[] = $listener->invoke($args);
            }
            return 
    $return;
        }
        
        
    /**
         * Get attached listeners
         * @return    array    IDelegate
         */
        
    public function getAttached()
        {
            return 
    $this->listeners;
        }

    PHP Code:
    /**
     * Handle groups of events
     * @author        Ezku (dmnEe0@gmail.com)
     * @since        26.9.2005
     */
    class EventHandler
    {
        private 
    $args = array();
        private 
    $events = array();
        
        
    /**
         * EventHandler consturctor
         * @param    mixed    default invocation argument
         * ...
         */
        
    public function __construct()
        {
            
    $this->args func_get_args();
        }
        
        
    /**
         * Attach a listener to an event
         * @param    string    event name
         * @param    object    IDelegate
         */
        
    public function attach($eventIDelegate $listener)
        {
            if(empty(
    $this->events[$event]))
            {
                
    $this->events[$event] = $this->getEvent();
            }
            
    $this->events[$event]->attach($listener);
        }
        
        
    /**
         * Trigger an event
         * @param    string    event name
         * @param    array    arguments, optional
         */
        
    public function trigger($event$args = array())
        {
            if(empty(
    $args))
            {
                
    $args $this->getArgs();
            }
            
    $return NULL;
            if(!empty(
    $this->events[$event]))
            {
                
    $return $this->events[$event]->trigger((array) $args);
            }
            return 
    $return;
        }
        
        
    /**
         * @return    array    default invocation arguments
         */
        
    protected function getArgs()
        {
            return 
    $this->args;
        }
        
        
    /**
         * @return    object    a fresh Event
         */
        
    protected function getEvent()
        {
            return new 
    Event;
        }
        
        
    /**
         * Shortuct for trigger()
         */
        
    public function __call($event$args)
        {
            return 
    call_user_func_array(array($this'trigger'), array($event$args));
        }
        
        
    /**
         * Shortcut for attach()
         */
        
    public function __set($event$args)
        {
            return 
    call_user_func_array(array($this'attach'), array($event$args));
        }
    }
    /**
     * Extend EventHandler to use MulticastEvents
     * 
     * @author        Ezku (dmnEe0@gmail.com)
     * @since        26.9.2005
     */
    class MulticastEventHandler extends EventHandler
    {
        protected function 
    getEvent()
        {
            return new 
    MulticastEvent;
        }

    It's supposed to be used like this (a total nonsense example, bear with me):

    PHP Code:
    $events = new EventHandler;
    $events->onload = new Delegate(new Handle('Controller'), 'execute');
    $events->onload(Context::getInstance); // lazy-loads and creates a Controller instance, calls execute() with the arguments given 
    Here's a slightly more elaborate example, albeit on an older version of the code.

    The whole idea is hacky to begin with, but aspect oriented programming in PHP seems doubly so to me. Just gives me a bad vibe.
    Business as usual is off the menu folks, ...

  2. #52
    Not yet perfect mattalexx's Avatar
    Join Date
    Oct 2005
    Location
    Taos, NM, US
    Posts
    441
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think this is one of the most appropiate post right now to post this stupid joke. Gimme a break, guys. I mean, really.
    Matt Alexander
    Alexander Site Design

  3. #53
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Off Topic:

    @mattalexx: it reminds me of the really old economist's joke: "If you lined up all the economists in the world, they would still point in all directions."

  4. #54
    SitePoint Enthusiast didimo's Avatar
    Join Date
    Jan 2006
    Posts
    79
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    here one yee can add to the list:

    use whitespace to not make the code appear cluttered!

    obviously dont over do this so the programmer is scrolling forever

    but this tip is good for most C-like languages

  5. #55
    SitePoint Guru
    Join Date
    May 2003
    Location
    virginia
    Posts
    988
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mattalexx
    I think this is one of the most appropiate post right now to post this stupid joke. Gimme a break, guys. I mean, really.
    ? Why are you reading this thread anyway ? I get what you are saying, but you spent too much time creating that last post.

  6. #56
    SitePoint Guru OfficeOfTheLaw's Avatar
    Join Date
    Apr 2004
    Location
    Quincy
    Posts
    636
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Joshua (the author of Refactoring to Patterns) recently released a nifty code smells to refactoring chart for those who'd be interested:

    http://industriallogic.com/papers/sm...factorings.pdf

    James Carr, Software Engineer


    assertEquals(newXPJob, you.ask(officeOfTheLaw));

  7. #57
    SitePoint Zealot Christiano's Avatar
    Join Date
    Apr 2005
    Location
    Belfast, North of Ireland
    Posts
    153
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I feel we are moving towards a total understanding of how code should be written. Modularity, Encapsulation, and other OO principles is gearing us towards a complete style of writing software regardless of environment and language.

    Id like to echo the post by Galo at the top of this page as being a prime example, we're getting better.
    Innovative Design and Development Services
    Design: XHTML - CSS - Photoshop CS - Flash 7 - Illustrator
    Development: Java - JavaScript - PHP - MySQL - .NET - Struts 2

  8. #58
    SitePoint Wizard REMIYA's Avatar
    Join Date
    May 2005
    Posts
    1,351
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by OfficeOfTheLaw
    Joshua (the author of Refactoring to Patterns) recently released a nifty code smells to refactoring chart for those who'd be interested:

    http://industriallogic.com/papers/sm...factorings.pdf
    It's a great link. 10x.

  9. #59
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    what are smell store factorings? I swear that's what I read at first
    I once had a problem.
    I thought: "Oh, I know: I'll just use XML!"
    Now I had two problems.

  10. #60
    SitePoint Addict
    Join Date
    Aug 2005
    Location
    Lithuania, Europe
    Posts
    295
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That's what I read as well

  11. #61
    SitePoint Member
    Join Date
    Apr 2006
    Posts
    9
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Coding "quality" has, in my opinion, as much to do with personal preferences as it has to do with actual performance.

    Some people prefer to use function names like "myFunctionName" and other use "my_function_name" or perhaps some people might even use another name scheme. Although I personally prefer to work with the underscores, all that really matters in coding is consistency. For instance; always refer to functions the same way.

    Classes etc can be a great tool when developing applications, however, recently I've noticed that a lot of developers just use class for anything. Two functions (that could easily be one function) are put in a class. Unnecessary object loading that is.

    I would always use central configuration files, langauge files, etc in my projects, just so that they all can be updated easily.

    The list really goes on and on. I'll defintely keep an eye on this thread, some useful stuff here.

  12. #62
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    12
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I had a older timer once told me...
    Commenting is good, but if you have more comments than code, you are writing a novel not a program.
    How can you tell if you are writing good code?
    Give it to a non programmer (okay maybe someone who have some logic sense), if they understand how your program flows and even what it does. You are writing good code.

  13. #63
    SitePoint Guru silver trophy Luke Redpath's Avatar
    Join Date
    Mar 2003
    Location
    London
    Posts
    794
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by REMIYA
    It's a great link. 10x.
    Indeed...especially number 3 on the chart..."Comments AKA Deodorant"

  14. #64
    SitePoint Member iceincube's Avatar
    Join Date
    May 2006
    Location
    Estonia
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    about naming variables(also objects ). I use prefixes.
    PHP Code:
    // boolean
    $bool_IsItTrue false;

    // integer
    $int_MyAge 19;

    // string
    $str_MyName 'iceincube';

    // object
    $obj_MyCat = new Cat('Garfield');

    // and so on ... 
    I hope that, my grammar isn't too bad. my native
    language is not english. TV teached me english

  15. #65
    SitePoint Addict
    Join Date
    Jan 2005
    Location
    Ireland
    Posts
    349
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by iceincube
    about naming variables(also objects ). I use prefixes.
    PHP Code:
    // boolean
    $bool_IsItTrue false;

    // integer
    $int_MyAge 19;

    // string
    $str_MyName 'iceincube';

    // object
    $obj_MyCat = new Cat('Garfield');

    // and so on ... 

    This really is not neccessary. I would assume that MyAge is an integer and that MyName was a string without any prefixes. Even MyCat's prefix is too verbose. Naming your variables smartly should mean the type is obvious as is the variables purpose. The context they are used in, the way they are defined, etc should also further verify their type.

  16. #66
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by iceincube
    about naming variables(also objects ). I use prefixes.
    I used to be a fan of hungarian notation, but it has lost it's luster with a dynamically typed language.

    What do you do with these?

    PHP Code:
    // maybe a string?
    $???_var '12';

    // okay, treat it like a number, now what is it?
    $???_var2 24 + $???_var;

    // okay, how about this?
    $???_var3 '46'.$???_var
    In the end, does it really matter?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  17. #67
    SitePoint Guru
    Join Date
    May 2005
    Location
    Finland
    Posts
    608
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dot-solutions
    Some people prefer to use function names like "myFunctionName" and other use "my_function_name" or perhaps some people might even use another name scheme. Although I personally prefer to work with the underscores, all that really matters in coding is consistency. For instance; always refer to functions the same way.
    I have a framework in which I've used camelCaps in the internals, but underscore_separated_names in API functions exposed to the user. This stems from the fact that I have no trouble with method names such as getInstance(), but dislike, say, pagecontrollers with methods like executeIndex(). Do you think this is bad practice and that I should just pick either one and stick to it?

  18. #68
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by sweatje
    I used to be a fan of hungarian notation, but it has lost it's luster with a dynamically typed language.
    That's because you use the wrong type of HN. Dynamically typed languages can still benefit from Apps Hungarian - but I cringe when I see PHP written with Systems Hungarian.

    That being said, I rarely use Apps Hungarian either, but it can be helpful.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev




  19. #69
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    comments != code quality
    classes != code quality

  20. #70
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'd say it's always good to give your variables good names that have a prefix of what type it is, for example:
    object= $oVar
    string = $sVar
    array = $aVar
    integer=$iVar
    and so on, I avoid using one letter variables. I only use those in for loops.

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  21. #71
    SitePoint Guru
    Join Date
    Aug 2005
    Posts
    986
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you give your variables meaningful names, it isn't necessary.

  22. #72
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If you do both, it's always a plus .
    You point out something that in this example doesn't look important, but in big codes can make a big difference, in one little character. So I would say: why not?

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  23. #73
    eschew sesquipedalians silver trophy sweatje's Avatar
    Join Date
    Jun 2003
    Location
    Iowa, USA
    Posts
    3,749
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by KoKoSNOOT
    ISo I would say: why not?
    Because it is trivially easy for your $iSomething to become a $sSomething due to business requirements, and now you ether have to change every occourance to match (=work) or leave incorrectly "typed" variable names all through your code (=easy, more likely). What will happen to the poor developer who comes along after you and actually believes your now incorrect notation?
    Jason Sweat ZCE - jsweat_php@yahoo.com
    Book: PHP Patterns
    Good Stuff: SimpleTest PHPUnit FireFox ADOdb YUI
    Detestable (adjective): software that isn't testable.

  24. #74
    SitePoint Addict Kokos's Avatar
    Join Date
    Nov 2005
    Location
    The Netherlands
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You got me there :P

    Taking over the web one pixel at a time.
    Currently working @ CodeCreators

  25. #75
    Web-coding NINJA! silver trophy beetle's Avatar
    Join Date
    Jul 2002
    Location
    Dallas, TX
    Posts
    2,900
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    KokoSNOOT - I suggest giving this a read

    http://www.joelonsoftware.com/articles/Wrong.html

    Specifically the section labeled "I'm Hungary" - but the whole thing is a good read anyway.
    beetle a.k.a. Peter Bailey
    blogs: php | prophp | security | design | zen | software
    refs: dhtml | gecko | prototype | phpdocs | unicode | charsets
    tools: ide | ftp | regex | ffdev





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
  •