SitePoint Sponsor

User Tag List

Results 1 to 25 of 116

Threaded View

  1. #1
    SitePoint Member
    Join Date
    Jun 2004
    Location
    London
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Dependency Injection in PHP4

    Hi,

    I've seen some real potential for using Dependency Injection, but all of the code I work with is PHP4 only. So, I thought I'd see if I could make something work in PHP4.

    The PHP5 features used by dependency injectors seem to be:
    • interfaces
    • type hints in constructor signature


    I replaced both of these with static methods on the class, e.g.

    Code:
    class Adder implements Number {
         function __construct(One one, Two two) {
         }
    }
    becomes

    Code:
    class Adder {
        function implements() {
            return array('Number');
        }
    
        function Adder($one, $two) {
        }
    
        function constructorSignature() {
             return array('One', 'Two');
        }
    }
    The DI container will call these methods when the class is registered to find out the class meta-information. Using this approach, I've ported Marcus's phemto code to php4. The tests run pretty much unmodified. Would be interested to hear what other people think of this approach.

    Dave
    Attached Files Attached Files


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
  •