SitePoint Sponsor

User Tag List

Page 2 of 2 FirstFirst 12
Results 26 to 36 of 36
  1. #26
    SitePoint Zealot Mau's Avatar
    Join Date
    Jan 2006
    Location
    California, USA
    Posts
    134
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I didn't intend to start an argument! I was just sharing some interesting results that I found. My hypothesis was that interfaces were faster, but that isn't the case if my test is accurate.

    By this,
    PHP has single inheritance.
    do you mean "Abstract classes support single inheritance while interfaces support multiple"?

    All in good intention and interested in learning more,

  2. #27
    SitePoint Guru Galo's Avatar
    Join Date
    May 2005
    Location
    Holland!
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Check this thread

    http://www.sitepoint.com/forums/show...ight=interface

    http://www.sitepoint.com/forums/showthread.php?t=288361

    Also another example to show you what an interface does
    Code:
    interface iOpenable{
          abstract function open();
          abstract function close();
    }
    
    <?php
    
    require_once('inc.openable.php');
    
    class Door implements iOpenable{
          
          private $_locked = false;
          
          public function open(){
                if($this->_locked){
                      print("Door is locked!");
                }else{
                      print("Opened door!")
                }
          }
    
          public function close(){
                print("BAF!");
          }
    
          //etc
    }
    
    class Jar implements iOpenable{
    
          private $_contents;
    
          public function __construct($contents){
                $this->_contents = $contents
          }
    
          public function open(){
                print("Opened Jar!");
          }
    
          public function close(){
                print("Closed Jar!");
          }
    }
    
    function openSomething(iOpenable $oOpenable){
          $oOpenable->open();
    }
    
    $oDoor = new Door();
    $oJar   = new Jar("milk");
    
    openSomething($oDoor);
    openSomething($oJar);
    
    ?>
    Example you don't need interfaces to write a complete framework
    http://www.sitepoint.com/forums/showthread.php?t=285844

    (just use base classes as abstraction)

    B.t.w, i Don't think PHP Interfaces are ready yet, no namespaces will result in non-unique context's where interface are "concrete" contracts between unique context's imho.

    Still PHP's Interfaces are weird, they alow you to specify abstract method declarations, loose types, abstration inherits interface.... all no good when it comes to understanding the use of interfaces...

    just my 2c
    Business as usual is off the menu folks, ...

  3. #28
    SitePoint Zealot agoossens's Avatar
    Join Date
    Mar 2004
    Location
    Adelaide, Australia
    Posts
    124
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mau
    do you mean "Abstract classes support single inheritance while interfaces support multiple"?
    You don't "inherit" an interface since there's no functionality to inherit. Instead, you "realise" or "implement" it (implementing expected methods vs. inheriting existing ones).

    PHP supports single class inheritance and multiple interface implementation.
    This space for rent.

  4. #29
    SitePoint Guru Galo's Avatar
    Join Date
    May 2005
    Location
    Holland!
    Posts
    852
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by agoossens
    You don't "inherit" an interface since there's no functionality to inherit. Instead, you "realise" or "implement" it (implementing expected methods vs. inheriting existing ones).

    PHP supports single class inheritance and multiple interface implementation.
    You Inherit is, oh yes you do, you inherit a "type" exacly....



    "PHP supports single class inheritance and multiple interface implementation."
    Well, support is a big word, it's more likely the zend team just stuffed some routines in the core so we could use something that's likely to act as a single interface unit, but it's not realy succesfull hence why we don't hear a lot of zend lately, but then again maybe there up to something..

    (start writing again from scratch)
    Business as usual is off the menu folks, ...

  5. #30
    SitePoint Addict
    Join Date
    May 2003
    Location
    The Netherlands
    Posts
    391
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Galo
    You Inherit is, oh yes you do, you inherit a "type" exacly....

    IMO type is implemented, you don't inherit squad by using interfaces.
    There’s more than one way to skin a cat.

  6. #31
    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 Mau
    Sorry, I didn't intend to start an argument! I was just sharing some interesting results that I found. My hypothesis was that interfaces were faster, but that isn't the case if my test is accurate.
    Fair enough. Just to make it clear - I didn't intend to patronize you and nor do I think any of the other posters were. I'm simply saying that your premise is wrong - It doesn't matter which is faster, since speed shouldn't be a decisive factor. (Unless of course the difference is very severe, but this isn't the case)

    Quote Originally Posted by Mau
    By this, do you mean "Abstract classes support single inheritance while interfaces support multiple"?
    Yes and no. You can only inherit once, but you can implement multiple interfaces.

  7. #32
    SitePoint Evangelist tetsuo shima's Avatar
    Join Date
    Oct 2005
    Location
    Switzerland
    Posts
    597
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I came to use interfaces as a way to ensure that classes have all the must-have functions. No more no less, at least not in PHP. I know, it's kind of limiting.
    The SEO Faq thread
    Dependency injection made easy: Phemto

  8. #33
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    As I have argued before, Interfaces in PHP make a great documentation, and that's it. They have no practical purpose whatesoever.

  9. #34
    SitePoint Member
    Join Date
    Nov 2005
    Posts
    10
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm just reading a book called "Holub on Patterns" (Java based code examples but I think the theory he talks about still applies).

    Chapter 2: "Programming with Interfaces and a Few Creational Patterns"
    Section: "The Fragile-Base-Class Problem"

    He argues that by extending a base class it becomes fragile because other derived implementations become obscured as programmers extend the interface defined by the base class. Whereas interfaces do not obscure the interface with implementation.

    I don't think I'm communicating his message very well, perhaps an example from the book will help.

    (The code here has been interpreted from a Java based example.)

    PHP Code:
    class ArrayList
    {
        
    // class implementation
        
    function add() { /*... */ }
        function 
    remove() { /*... */ }
        function 
    clear() { /*... */ }
    }

    class 
    Stack extends ArrayList
    {
        private 
    topOfStack 0;
        
        public 
    push($article)
        {
            
    $this->add($this->topOfStack++, $article);
        }
        
        public 
    pop()
        {
            
    $this->remove(--$this->topOfStack);
        }
        
        public 
    pushMany($articles)
        {
            foreach (
    $articles as $article) {
                
    $this->push($article);
            }
        }
    }

    $stack = new Stack();
    $stack->push("1");
    $stack->push("2");
    $stack->clear();
    $stack->push("3"); 
    ...since the base class doesn't know anything about the index of the item at the top of the stack (topOfStack), the Stack object is now in an undefined state. The next call to push() puts the new item at index 2 (the current value of the topOfStack), so the stack effectively has three elements on it, the bottom two of which are garbage.
    One (hideously bad) solution to the inheriting-undesirable-methods problem is for Stack to override all the methods of ArrayList that can modify the state of the array to manipulate the stack pointer. This is a lot of work, though, and doesn't handle problems such as adding a method like clear() to the base class after you've written the derived class.
    Quote by Allen Holub from "Holub on Patterns" (page 41 to 42).

    He goes onto to say that the programmer shouldn't exclude the use of base classes, which are very powerful, but carefully consider their use. They can be extremely useful to share functionality but can also cause code like the example described above. He then describes how interfaces negate this and other problems at the cost of losing the shared implementation from the base class.

    [edit] Here's a suggested solution using interfaces
    PHP Code:
    interface Stack
    {
        public function 
    push($article);
        public function 
    pop();
        public function 
    pushMany($articles);
        public function 
    size();
    }

    class 
    SimpleStack implements Stack
    {
        private 
    $topOfStack;
        private 
    $arrayStack;

        function 
    __construct()
        {
            
    $this->arrayStack = new ArrayStack();
        }

        function 
    push($article)
        {
            
    $this->arrayStack->add($this->topOfStack++, $article);
        }

        
    // you can work out the rest...

    Hope that helps someone.

    Luke

  10. #35
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Basically one should always keep in mind two things:

    1) OOP is all about types.
    2) Types are all about behavior.

    This also tell us that classes are a foreign entity in OOP, probably introduced in C++ to ease transition from C. Interfaces -- as a theoretical, not a syntactical concept -- are what defines a type, and a class is really just a convenience allowing us to define implementation. In reality we don't need classes, nor interfaces as formal entities, to have an OOP language.

    I think we just got Javascript.

  11. #36
    SitePoint Addict
    Join Date
    Aug 2003
    Location
    Toronto
    Posts
    300
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    3) There is not One True Way


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
  •