SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 56
  1. #1
    SitePoint Enthusiast boroda's Avatar
    Join Date
    Jan 2006
    Location
    Moscow, Russia
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Where to place utility functions in the OO application

    Dear friends,

    Just another question to ask your experience. May be this question is the result of the experience lack in OOP.

    In my framevork almost every function is encapsulated inside the classes. But there is small set of the utility functions such as

    PHP Code:
    function ifset(&$myvar,$def='')
        {
            if (isset(
    $myvar) && is_null($myvar)) {
                return 
    $def;
            } elseif (isset(
    $myvar)) {
                return 
    $myvar;
            } else {
                return 
    $def;
            }
        } 
    or

    PHP Code:
    function get_var_with_def ($name,$def='')
        {
            
    $myvar=$def;
            if (isset (
    $_REQUEST[$name])) {
                
    $myvar=$_REQUEST[$name];
            } else {
                
    $myvar=$def;
            }
            return 
    $myvar;
        } 
    which lives in the global space. It is something confusion to me, because this functions can interfere with 3-rd party libs or code.

    The first way which come in mind is to add prefix to this functions, or isolate them in some class, so the function call will looks like

    myframeworkUtility::ifset($t);

    or

    myframework_ifset($t);

    Both way are too ugly. Is there another convenient way to solve this problem?

    Thank you.

  2. #2
    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 boroda
    In my framevork almost every function is encapsulated inside the classes. But there is small set of the utility functions such as
    The root of your problem is that you are thinking inside a procedural paradigm, which can be very hard to unlearn once you got it in. Rather than thinking in data+function you should be thinking in objects and responsibilities. One way could be to replace this :
    PHP Code:
    $foo ifset($_REQUEST['foo']); 
    With something like this :
    PHP Code:
    class Request
    {
        function 
    get($property$default "") {
            return isset(
    $_REQUEST[$property]) ? $_REQUEST[$property] : $default;
        }
    }
    ...
    $request =& new Request();
    $foo $request->get('foo'); 
    That's a general rule though, and in a language like PHP it may not always make the most sense to follow a strict OO paradigm. PHP has a lot of built-in functions which are clearly procedural in nature, so you will always have to mix the two up.

  3. #3
    SitePoint Enthusiast boroda's Avatar
    Join Date
    Jan 2006
    Location
    Moscow, Russia
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you, kyberfabrikken.

    Quote Originally Posted by kyberfabrikken
    The root of your problem is that you are thinking inside a procedural paradigm, which can be very hard to unlearn once you got it in.
    Exactly. But I'm trying to unlearn. It is not easy indeed.

    I've got your example. But another question arrives

    So I'll have to implement

    PHP Code:
     function get($property$default "") { 
            return isset(
    $_REQUEST[$property]) ? $_REQUEST[$property] : $default
        } 
    In every object, i.e. Request, ActiveRecord and so on. I still want to write function ifset instinctively just to use in the different objects.

    Could you, please, give me advise how to deal with this?

    Thank you.

  4. #4
    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 boroda
    So I'll have to implement
    (...)
    In every object, i.e. Request, ActiveRecord and so on. I still want to write function ifset instinctively just to use in the different objects.

    Could you, please, give me advise how to deal with this?
    You could make a general-purpose wrapper for arrays, which adds the functionality. A popular solution to that particular problem is the dataspace. In it's simplest form, you'll make a class, which has an array as a private member :
    PHP Code:
    class DataSpace
    {
        var 
    $_data;
        function 
    DataSpace(&$array) {
            
    $this->_data =& $array;
        }

        function 
    get($property$default NULL) {
            return isset(
    $this->_data[$property]) ? $this->_data[$property] : $default;
        }

        function 
    set($property$value) {
            
    $this->_data[$property] = $value;
        }
    }
    ...
    $request =& new DataSpace($_REQUEST);
    $row =& new DataSpace(mysql_fetch_assoc($result));
    // etc ... 

  5. #5
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In every object, i.e. Request, ActiveRecord and so on.
    No, you wouldn't

    What you are looking for is an Interface, and for an abstract class which has the basic implementation, that follows that said Interface; I do just that for my Dataspace abstraction, thus such responsibility as a Request uses the abstraction.

    The Request class ends up with little implementation in that event. My Session class on the other hand, requires that data is stored to a session, as apposed to the class properties directly, so the Session class just implements the Interface, and nothing more as another example?

    A Parameters class which accepts an array through the constructor uses the Dataspace abstraction as well, since the constructor isn't declared through the Interface; The Dataspace abstraction for an import accepts only it's self, an IDataspace type.

    Hope that helps you anyways

    PHP Code:
    interface IDataspace {
            public function 
    set();
            public function 
    get();
            public function 
    has();
        }
        
        abstract class 
    Dataspace implements IDataspace {
            protected 
    $parameters = array();
            
            public function 
    __construct() {}
            public function 
    set() {
                
    $parameters func_get_args();
                if( 
    is_array$parameters ) ) {
                    
    $this -> parameters[strtolowerarray_shift$parameters ) )] = array_shift$parameters );
                }
            }
            
            public function 
    get() {
                
    $parameters func_get_args();
                if( 
    is_array$parameters ) ) {
                    if( 
    array_key_exists$parameter array_shift$parameters ), $this -> parameters ) ) {
                        return 
    $this -> parameters[$parameter];
                    }
                }
                return 
    false;
            }
            
            public function 
    has() {
                
    $parameters func_get_args();
                if( 
    is_array$parameters ) ) {
                    if( 
    array_key_exists$parameter array_shift$parameters ), $this -> parameters ) && !empty( $this -> parameters[$parameter] ) ) {
                        return 
    true;
                    }
                }
                return 
    false;
            }
            
            public function 
    importIDataspace $dataspace ) {
                
    $this -> parameters array_merge$this -> parameters$dataspace -> export() );
            }
            
            public function 
    export() {
                return 
    $this -> parameters;
            }
        }

    class 
    Parameters extends Dataspace {
            public function 
    __construct$parameters ) {
                foreach( 
    $parameters as $parameter => $value ) {
                    
    $this -> set$parameter$value );
                }
            }
        }

    class 
    Context extends Dataspace {
            public function 
    __construct() {}
            
        }

    class 
    Session implements IDataspace {
            public function 
    __construct() {}
            public function 
    set() {
                
    $parameters func_get_args(); 
                if( 
    is_array$parameters ) ) {
                    
    $_SESSION[strtolowerarray_shift$parameters ) )] = array_shift$parameters );
                }
            }
            
            public function 
    get() {
                
    $parameters func_get_args();
                if( 
    is_array$parameters ) ) {
                    if( 
    array_key_exists$parameter array_shift$parameters ), $_SESSION ) ) {
                        return 
    $_SESSION[$parameter];
                    }
                }
                return 
    false;
            }
            
            public function 
    has() {
                
    $parameters func_get_args();
                if( 
    is_array$parameters ) ) {
                    if( 
    array_key_exists$parameter array_shift$parameters ), $_SESSION ) && !empty( $_SESSION[$parameter] ) ) {
                        return 
    true;
                    }
                }
                return 
    false;
            }
            
            public function 
    destroy() {
                unset( 
    $_SESSION );
                
    $_SESSION = array();
            }
        }

    class 
    Request extends Dataspace {
            public function 
    __construct() {
                foreach( 
    $this -> sanitisearray_merge$_GET$_POST ) ) as $parameter => $value ) {
                    
    $parameter strtolower$parameter );
                    
    $this -> setstrtolower$parameter ), $value );
                } 
                
    parent::__construct();
            }
            
            public function 
    requestMethod() {
                return ( 
    strcasecmp$_SERVER['REQUEST_METHOD'], 'POST' ) == )? 'POST':'GET';
            }
            
            public function 
    setSession() {
                
    $parameters func_get_args();
                if( 
    is_array$parameters ) ) {
                    return 
    $this -> get'session' ) -> setarray_shift$parameters ), array_shift$parameters ) );
                }
                return 
    false;
            }
            
            public function 
    getSession() {
                
    $parameters func_get_args();
                if( 
    is_array$parameters ) ) {
                    return 
    $this -> get'session' ) -> getarray_shift$parameters ) );
                }
                return 
    false;
            }
            
            public function 
    getLang() {
                return 
    'en';
            }
            
            protected function 
    sanitise$parameters ) {
                if( 
    is_array$parameters ) ) {
                    
    $tmp = array();
                    foreach( 
    $parameters as $parameter => $value ) { 
                        if( 
    get_magic_quotes_gpc() ) { 
                            if( 
    is_array$value ) ) {
                                
    $value $this -> sanitise$value );
                            } else {
                                
    $value stripslashes$value );
                            }
                        }
                        
    $tmp[$parameter] = $value;
                    }
                    return 
    $tmp;
                }
            }
        }

    class 
    PageModel extends Dataspace {
            private 
    $conn;
            
            public function 
    __constructIConnection $conn ) {
                
    $this -> conn $conn;
            }
        }

    // ...
    public function executeContext $contextIDataspace $dataspace ) { 
                
    $model = new PageModel$context -> get'db' ) );
                
    $model -> import$dataspace );
    // ...
    $view -> output$model -> export() );
    // ...


  6. #6
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    ...

    Also, since there are times that you are dealing with arrays directly, as in the use of the Parameter class, there may well be times when you are dealing with another structure as well, that you need to provide accessability to it, such as XML or a resource such as File or Database yes?

    Your client scripts wouldn't be reusable if they dealt with the structure directly, so you could instead have classes to manage those structures, such as...

    PHP Code:
    class XmlParameters extends Parameters {}
    class 
    FileParameters extends Parameters {}
    class 
    SqlParameters extends Parameters {}
    // etc 
    In old script which is now redundant in my case, I named Parameters as DataSource but from earlier discussions on these forums, I changed the naming, which on reflection, appears to be more appropriate

    PHP Code:
    /**
         * Package to provide a means to easily store and access a row of data, 
         * via an object interface. Where the data source has an inaccessable or an 
         * inappropriate structure.
         * @package data source
         * @author les quinn <nukebaghdad@yahoo.co.uk>
         * @version 1.0-beta
         * @copyright copyright (c)2005 les quinn all rights reserved
         */
         
        
    interface IDataSource {
            public function 
    __construct$row );
            public function 
    getInteger$parameter );
            public function 
    getParameter$parameter );
        }
        
        abstract class 
    DataSource implements IDataSource {
            protected 
    $parameters = array();
            
    /**
             * DataSource::__construct();
             * Invoke an abstract class method to manage a row.
             * @param mixed $row row of data
             * @access public
             * @return void
             */
            
    public function __construct$row ) {
                
    $this -> invoke$row );
            }
            
    /**
             * DataSource::getInteger();
             * Return request parameter if it is an integer, as an integer.
             * @param string $parameter parameter to fetch
             * @access public
             * @return int | bool return requested parameter, else return false
             */
            
    public function getInteger$parameter ) {
                if( 
    array_key_exists$parameter$this -> parameters ) ) { 
                    if( 
    is_int$this -> parameters[$parameter] ) ) { 
                        return 
    $this -> parameters[$parameter];
                    } else {
                        
    // final resort to determine if parameter is an integer
                        
    if( preg_match'/^[0-9]+$/'$this -> parameters[$parameter] ) ) {
                            return 
    $this -> parameters[$parameter];
                        }
                    }
                }
                return 
    false;
            }
            
    /**
             * DataSource::getParameter();
             * Return requested parameter as is.
             * @param string $parameter parameter to fetch
             * @access public
             * @return mixed return requested parameter, else return false
             */
            
    public function getParameter$parameter ) {
                if( 
    array_key_exists$parameter$this -> parameters ) ) {
                    return 
    $this -> parameters[$parameter];
                }
                return 
    false;
            }
            
    /**
             * DataSource::invoke();
             * @abstract
             * @param mixed $row row of data
             * @access protected
             * @return void
             */
            
    abstract protected function invoke$row );
        }
        
        class 
    XmlDataSource extends DataSource {
            
    /**
             * XmlDataSource::__construct();
             * @param DomElement $row row of data
             * @access public
             * @return void
             */
            
    public function __construct$row ) {
                
    parent::__construct$row );
            }
            
    /**
             * XmlDataSource::invoke();
             * Invoke row of data as class properties.
             * @param DomElement $row row of data
             * @access protected
             * @return void
             */
            
    protected function invoke$row ) {
                if( 
    $row instanceof DomElement ) {
                    if( 
    $row -> hasChildNodes() ) {
                        foreach( 
    $row -> childNodes as $key => $parameter ) {
                            if( 
    $parameter -> nodeType == XML_ELEMENT_NODE ) {
                                
    $this -> parameters[strtolower$parameter -> nodeName )] = $parameter -> nodeValue;
                            }
                        }
                    }
                }
            }
        }

        class 
    SqlDataSource extends DataSource {
            public function 
    __construct$row ) {
                
    parent::__construct$row );
            }
            
            protected function 
    invoke$row ) {
                return 
    false;
            }
        }
        
        class 
    ArrayDataSource extends DataSource {
            public function 
    __construct$row ) {
                
    parent::__construct$row );
            }
            
            protected function 
    invoke$row ) {
                foreach( 
    $row as $key => $parameter ) {
                    
    $this -> parameters[strtolower$key )] = $parameter;
                }
            }
        } 

  7. #7
    SitePoint Enthusiast boroda's Avatar
    Join Date
    Jan 2006
    Location
    Moscow, Russia
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank you.

    Usefull information and interesting examples. I think it is enough in my case.

    But theoretically I can guess that this approach sometimes will require multiple inheritance -- in the case when the basic functionality will be spreaded in several base classes.

  8. #8
    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 boroda
    But theoretically I can guess that this approach sometimes will require multiple inheritance -- in the case when the basic functionality will be spreaded in several base classes.
    You can't use multiple inheritance in PHP, and for the better. If your classes are so complex that they need to draw on multiple sources of basic functionality, you must use composition. In theese cases, Interfaces are a really good tool to guide you. For example, you might want to implement DataSpace as an interface (as Livingston did it above), and then use composition behind the scenes. This is also referred to as a decorator.
    Note that PHP4 doesn't support interfaces - you'll have to use PHP5, which is a good idea anyway, if you're going to write OO code.

  9. #9
    SitePoint Wizard Mike Borozdin's Avatar
    Join Date
    Oct 2002
    Location
    Edinburgh, UK
    Posts
    1,743
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by boroda
    myframeworkUtility::ifset($t);
    or
    myframework_ifset($t);
    Both way are too ugly.
    I don't think so. I have some functions that actually doesn't belong to any class, so I put to the to the class called Utils. Just use Utils::yourStaticFunction()

  10. #10
    SitePoint Enthusiast boroda's Avatar
    Join Date
    Jan 2006
    Location
    Moscow, Russia
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kyberfabrikken, Dr Livingston

    Thank you once again for examples... I've just realised how much code should I refactor yo just reimplement ifset function in OO paradigm. It is awfull, but seems that I should invest this time to the project, becuase myframeworkUtility::ifset($t); looks like a hack to the entire class model, however it is much simplier to implement (just rename all ifset calls in the code base).

    Quote Originally Posted by kyberfabrikken
    Note that PHP4 doesn't support interfaces - you'll have to use PHP5, which is a good idea anyway, if you're going to write OO code.
    I know. But for now it is important for me to understand the principles and methodics. In any way i plan to switch to php5 this year.

    Mike Borozdin,
    Hello from Moscow

  11. #11
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    Note that PHP4 doesn't support interfaces - you'll have to use PHP5, which is a good idea anyway, if you're going to write OO code.
    Also note that in PHP you don't actually need formal interfaces, except for documentation purposes. Use duck typing instead.

  12. #12
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quack... Quack?

    No, seriously though, Interfaces do play a much more important role, other than just documentation, I'm afraid to say...

    For those who don't know any better, in regards to Multiple Inheritance, and why we can do without it,

    http://www.javaworld.com/javaworld/j...3-diamond.html, and http://www.javaworld.com/javaworld/j...echniques.html are for reading
    Last edited by Dr Livingston; Jan 29, 2006 at 11:10. Reason: blea...

  13. #13
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    Quack... Quack?

    No, seriously though, Interfaces do play a much more important role, other than just documentation, I'm afraid to say...

    For those who don't know any better, in regards to Multiple Inheritance, and why we can do without it,

    http://www.javaworld.com/javaworld/j...3-diamond.html, and http://www.javaworld.com/javaworld/j...echniques.html are for reading
    Dear Doctor, don't forget that we're talking about PHP, which is a dynamic language.

    In Java, Interfaces are invented to work around the language's single-class-inheritance limitation (which was indeed introduced for good reasons), because as a statically-typed language Java requires each variable to have a declared type.

    In PHP, as well as in other dynamically typed, interpreted languages like Python, Ruby and Javascript, you don't need a formal Interface to ensure that you're passing a correctly typed variable at compile time, as there is no compile time.

    To put things more simply and related to PHP: if you declare an Interface and have a method that requires a parameter variable to implement that Interface (via type hinting), you will get an error if you try to pass an object which doesn't; but if you don't require a certain interface from that parameter, and you pass an object which doesn't have a method you need, you'll also get an error, but this time it will be much more informative.

    There is a difference between an interface, which is essentially the collection of all public members of a class, and an Interface, which is a formal entity in Java and PHP (and C++?) used to force an object to implement a certain interface (with the lowercase "I"). The former are one of the basic property of all programming, but the latter are superfluous in dynamically-typed languages.

    My personal stand on Interfaces in PHP is this:

    1. Define well your interfaces (lowercase) and make sure that they are implemented at the appropriate classes.

    2. Try not to use Interfaces (capital); duck typing works just as well, if not better.

    3. If your application will be used widely (e.g. a framework), Interfaces (capital) might be a good way to document interfaces (lowercase). However,

    4. Never, ever use type hinting.

  14. #14
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Umm...

    Well, I'm not going to make an arguement about this as it's not fair to the threads author, as it's going off topic, that and that this has been discussed before a few times, and every time it's got heated

    Can we both agree to disagree, for the sake of this thread? Thanks

  15. #15
    SitePoint Enthusiast boroda's Avatar
    Join Date
    Jan 2006
    Location
    Moscow, Russia
    Posts
    61
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    kyberfabrikken, Dr Livingston

    3 Hours of heavy refactoring and I got the things working. Uff....

    May be it looks like there is some overhead, but I like the way this approach works.

    Thank you.

  16. #16
    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 BerislavLopac
    4. Never, ever use type hinting.
    I'm a bit ambivalent towards this issue. One one hand I agree that in a dynamically typed langauge, type hinting is a superflous restriction. On the other hand, the Interface (with a capital I) can help you catch errors, and at the same time, it's a good syntax for describing the interface (with a lowercase i). By using Interfaces you force the programmer to be aware of the interface. If you didn't use the mechanism, you have to trust that the programmer keeps his code updated, when the interface change etc.
    It's sort of parallel to unittests. You could do without it, but by having them, you enforce the rules - If the programmer miss something, the code simply won't run/pass.
    Typehinting are a good companion for DI, so for that reason alone, I might want to use Interfaces.

  17. #17
    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 boroda
    Thank you once again for examples... I've just realised how much code should I refactor yo just reimplement ifset function in OO paradigm. It is awfull, but seems that I should invest this time to the project, becuase myframeworkUtility::ifset($t); looks like a hack to the entire class model, however it is much simplier to implement (just rename all ifset calls in the code base).
    Changing the paradigm, means changing the very structure of your program. It would be equaly hard to go the opposite direction.
    You really shouldn't think about it in terms of "how to implement ifset in OO" - It's rather "how would I ensure that undefined variables get meaningful defaults". The answer in procedural programming is to implement a function ifset, while in OO programming the answer is to encapsulate the data in an object, and provide getters/setters that do the checking.
    OO isn't just a different syntax - it's a different way of solving problems.

  18. #18
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    PHP Code:
    ...
     
    $parameters func_get_args();
     if( 
    is_array$parameters ) ) {
         ...
     }
     ... 
    Just to nitpick: you could probably do without is_array() as func_get_args() always returns an array.

  19. #19
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    as func_get_args() always returns an array.
    Yes, I'm well aware of that You are the second person to point that out but I have left the check in there for a reason; The underlying script makes use of an array, and I want the array be be asured that it's actually an array...

    Who is to say that the library function I use won't change in the future? Proberly not I know, but there is that possibility (it happens) so I don't want my scripts to break if there is anything, other than an array, if that makes sense?

    I hope it does, as I feel more comfortable just with that check being there, presently, and further down the road

    Kyber, +1

  20. #20
    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)
    As a follow-up on the interfaces vs. Interfaces thing, I just stumbled on this suggestion for another weakly typed language :
    http://www.haxe.org/proposals/signatures_weak_interface
    Perhaps we should have had signatures in php rather than interfaces.

    Could anyone btw verify/refute whether signature is a widely used term, or something the author made up ?

  21. #21
    SitePoint Guru BerislavLopac's Avatar
    Join Date
    Sep 2004
    Location
    Zagreb, Croatia
    Posts
    830
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kyberfabrikken
    As a follow-up on the interfaces vs. Interfaces thing, I just stumbled on this suggestion for another weakly typed language :
    http://www.haxe.org/proposals/signatures_weak_interface
    Perhaps we should have had signatures in php rather than interfaces.
    This is an interesting idea, and the haXe also looks interesting; seems similar to Javascript 2.0.

    Quote Originally Posted by kyberfabrikken
    Could anyone btw verify/refute whether signature is a widely used term, or something the author made up ?
    "Signature" denotes the details of a method (its name, parameters and return value), so I don't think this is a good term here. "Interface" still seems the most obvious choice...

  22. #22
    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 kyberfabrikken
    Could anyone btw verify/refute whether signature is a widely used term, or something the author made up ?
    http://en.wikipedia.org/wiki/Method_signature
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais

  23. #23
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What we really need are multimethods and guards.
    Code:
    class User {
    	function save(DB $db) 
    		{ save to db }
    	function save(XML $xml) 
    		{ save to xml stream }
    	function save($s | $s->implements('write'))
    		{ save to something that has 'write' function  }
    }
    would be nice, won't it?

  24. #24
    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 BerislavLopac
    This is an interesting idea, and the haXe also looks interesting; seems similar to Javascript 2.0.
    As far as I understand it, haXe is a framework of languages (sort of like .NET) based on a VM called neko. It is planned to make haXe support both javascript and actionscript (js2.0). It's in its early states though.

    Quote Originally Posted by BerislavLopac
    "Signature" denotes the details of a method (its name, parameters and return value), so I don't think this is a good term here. "Interface" still seems the most obvious choice...
    Quote Originally Posted by dagfinn
    Thanks. I meant if the term was used as a synonym for "weak interfaces" though. But I guess it's not.

    Quote Originally Posted by stereofrog
    Code:
    	function save($s | $s->implements('write'))
    		{ save to something that has 'write' function  }
    Is this something you made up, or do you know of any languages which would allow that ?

  25. #25
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Dr Livingston
    I hope it does, as I feel more comfortable just with that check being there, presently, and further down the road
    Sure. I guess I trust the PHP devs blindly for not changing such fundamental behaviour


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
  •