SitePoint Sponsor

User Tag List

Page 1 of 3 123 LastLast
Results 1 to 25 of 62

Hybrid View

  1. #1
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)

    Is PHP's OOP implementation complete?

    Quote Originally Posted by CS Geek View Post
    So, is PHP the best choice if I'm just starting? and is it good to develop big social websites in it?
    If you are just starting, PHP has insurmountable advantage over other programming languages for the webs, for reasons I already stated above. For a real big social website, the difference may not be that significant, and in fact PHP does lack certain OOP features to be called a complete OO language. In this case, you will have to decide for yourself.

  2. #2
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Hall of Famer View Post
    PHP does lack certain OOP features to be called a complete OO language.
    Like what, for example? Or compared to what?
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  3. #3
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Force Flow View Post
    Like what, for example? Or compared to what?
    Well, heres a list I can think of:

    1. Not everything is an object, string and array for instance.
    2. Impossible to achieve auto-boxing/auto-unboxing.
    3. No nested/anonymous class support.
    4. C# style property accessors syntax aint implemented.
    5. Not supporting wildcard namespace import like Java and C#.
    6. Metaprogramming not available, runkits may be a solution to this?

    Of course there are more, I may add them to the list later.

  4. #4
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    A programming language is Object Oriented if it satisfies 3 base principles:
    1. Encapsulation
    2. Polymorphism
    3. Inheritance

    Since PHP does satisfy all three, it doesn't matter what *some* think of *how much* object oriented it is or isn't!

    Quote Originally Posted by Hall of Famer View Post
    1. Not everything is an object, string and array for instance.
    In PHP every var is either an object, number, string or array. What else do you think they are? Also, not everything needs to be an object for it to be an object oriented language.

    Quote Originally Posted by Hall of Famer View Post
    3. No nested/anonymous class support.
    PHP has had anonymous classes for ages; they're more for using with properties than methods though.

    Quote Originally Posted by Hall of Famer View Post
    4. C# style property accessors syntax aint implemented.
    Is that a requirement somewhere that a language has to have C# style way of things to be qualified as an object oriented language? There were object oriented languages before there was C#.

    Quote Originally Posted by Hall of Famer View Post
    5. Not supporting wildcard namespace import like Java and C#.
    Namespaces are a bit new in PHP, they'll mature with time.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  5. #5
    SitePoint Member
    Join Date
    Aug 2013
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by asp_funda View Post
    A programming language is Object Oriented if it satisfies 3 base principles:
    1. Encapsulation
    2. Polymorphism
    3. Inheritance
    I'm new to OOP, can you help me understand what each of those principles are?

    P.S. I looked at the Syntax Hiliter in your sig for WP, did you code that?

  6. #6
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by c.nickerson View Post
    I'm new to OOP, can you help me understand what each of those principles are?
    http://en.wikipedia.org/wiki/Encapsu...programming%29
    http://en.wikipedia.org/wiki/Polymor...ed_programming
    http://en.wikipedia.org/wiki/Inherit...programming%29
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  7. #7
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by c.nickerson View Post
    P.S. I looked at the Syntax Hiliter in your sig for WP, did you code that?
    Yup
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  8. #8
    SitePoint Guru bronze trophy
    Join Date
    Dec 2003
    Location
    Poland
    Posts
    930
    Mentioned
    7 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Hall of Famer View Post
    1. Not everything is an object, string and array for instance.
    I don't think I would want this in PHP considering the fact that objects in PHP result in a pretty performance overhead. Primitive types like strings and arrays are used very often so this could have tangible negative consequences.

    For example, PHP 5 introduced DateTime class with their methods - they are very convenient but when I used them for date and time conversions where I had to create a DateTime object and a DateTimeZone object for each conversion - when I put in a loop it resulted in a huge performance penalty on pages with hundreds of dates. I certainly wouldn't want to deal with such problems while using strings and arrays. Maybe if PHP were a compiled language, then yes.

    PHP must be practical for being used on the web so performance is very important - it's all about compromise.

  9. #9
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Lemon Juice View Post
    I don't think I would want this in PHP considering the fact that objects in PHP result in a pretty performance overhead. Primitive types like strings and arrays are used very often so this could have tangible negative consequences.

    For example, PHP 5 introduced DateTime class with their methods - they are very convenient but when I used them for date and time conversions where I had to create a DateTime object and a DateTimeZone object for each conversion - when I put in a loop it resulted in a huge performance penalty on pages with hundreds of dates. I certainly wouldn't want to deal with such problems while using strings and arrays. Maybe if PHP were a compiled language, then yes.

    PHP must be practical for being used on the web so performance is very important - it's all about compromise.
    Well you have a point when it comes to certain data types such as primitive numbers and boolean values, but Id still say a good object oriented language needs at least Strings and Arrays to be objects. Every well-known object oriented programming language does it this way, in Java and C# strings and arrays are objects, while in Ruby everything is an object(and for some reason, the overhead issue does not seem to be a big concern). Not sure with Python though, but its also considered a more object oriented language. PHP needs to catch up on this aspect, I like what features they've brought in but its not sufficient yet. On the other hand, I doubt PHP's DateTime class is even fully optimized at this point, which may be part of the cause of your problem?

  10. #10
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    4 is proposed for PHP 5.6 as well

  11. #11
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Stormrider View Post
    4 is proposed for PHP 5.6 as well
    I wonder what else are proposed for PHP 5.6 though. I believe Metaprogramming is needed for PHP 5.6, the others can wait till PHP 6.0. The easiest way to get it done is to simply have runkit bundled with PHP like PDO and Memcached. It should not even be difficult, as all they have to do is to improve runkit's compatibility beyond PHP 5.3. I know runkit does not fully achieve metaprogramming, but it will help.

  12. #12
    SitePoint Enthusiast stelleninfotech's Avatar
    Join Date
    Apr 2013
    Location
    India
    Posts
    62
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP has many advantages over its competitors as web development through PHP is easier, fast and economic. That,s why it is widely preferred.

  13. #13
    SitePoint Wizard silver trophybronze trophy Stormrider's Avatar
    Join Date
    Sep 2006
    Location
    Nottingham, UK
    Posts
    3,133
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Does PHP need that? Why is that so urgent?

  14. #14
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Stormrider View Post
    Does PHP need that? Why is that so urgent?
    Yes PHP needs that, it makes plugin system so much easier to design.

  15. #15
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    /* ======================
     * 1 Read only attribute on member variables would save having to create numerous getters
     */
    class Buffy { public readonly $slainVampireCount: }

    /* ======================
     * 2 An immutable function which throws an exception on any write attempts
     * Basically a value object.  But be nice to have it apply to arrays as well.  Helps to make code more robust.
     */
    $buffy immutable(new Buffy('Buffy Summers'14304));
     */

    /* ======================
     * Type hinting and automatic getter/setter generator for member variables?
     * Already type hint for methods so why not for member variables
     *
     * Being able to specify standard getter/setters will drastically reduce boiler plate code as well as documentation annotations
     * Bonus points if type safe arrays can be handled as well
     */
    class Buffy 
        private 
    Address $address :  get;  // Functions as though getAddress() available
        
    private Person $fans: array, add// Collection with $fans->add(Person $person)  available

    /* ======================
     * Type hinting return values
     * Mostly for documentation but still handy
     */
    function Person getGreatestVampireSlayerEver() { return new Person('Buffy'); } 
    None of these are actually required or urgent but I think they would improve the quality of code and not be all that difficult to implement.

  16. #16
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by ahundiak View Post
    PHP Code:
    /* ======================
     * 1 Read only attribute on member variables would save having to create numerous getters
     */
    class Buffy { public readonly $slainVampireCount: }

    /* ======================
     * 2 An immutable function which throws an exception on any write attempts
     * Basically a value object.  But be nice to have it apply to arrays as well.  Helps to make code more robust.
     */
    $buffy immutable(new Buffy('Buffy Summers'14304));
     */

    /* ======================
     * Type hinting and automatic getter/setter generator for member variables?
     * Already type hint for methods so why not for member variables
     *
     * Being able to specify standard getter/setters will drastically reduce boiler plate code as well as documentation annotations
     * Bonus points if type safe arrays can be handled as well
     */
    class Buffy 
        private 
    Address $address :  get;  // Functions as though getAddress() available
        
    private Person $fans: array, add// Collection with $fans->add(Person $person)  available

    /* ======================
     * Type hinting return values
     * Mostly for documentation but still handy
     */
    function Person getGreatestVampireSlayerEver() { return new Person('Buffy'); } 
    None of these are actually required or urgent but I think they would improve the quality of code and not be all that difficult to implement.
    If you want a read-only variable, use const.

    For getters/setters, you can use the magic methods __get() and __set(). Or, define your own functions. Or, make the variables public and access them through the object.

    PHP Code:
    class thing {
    public 
    $attribute=null;


    $newthing = new thing();
    $newthing->attribute='blue';
    echo 
    $newthing->attribute
    If you want documentation to appear, use PHPdoc syntax, which can be parsed in most IDEs that supports PHP.

    PHP Code:
    /**
    * function description here
    * @param string $input some comments here
    * @return string some comments here
    */
    function testme($input){
    return 
    $input;

    I think some of this boils down to partly unfamiliarity with PHP and partly wanting it to be something it is not.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  17. #17
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    If you want a read-only variable, use const.
    You have to define a const with class definition and its value can't be changed. Read-only on the other hand can be set in a method (its rw within the class) but is read-only when accessed outside the class. That would definitely bring down the usage of getters.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  18. #18
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by asp_funda View Post
    You have to define a const with class definition and its value can't be changed. Read-only on the other hand can be set in a method (its rw within the class) but is read-only when accessed outside the class. That would definitely bring down the usage of getters.
    Good point. Also const does not accept arrays or objects, it only works for numbers, booleans and strings.

  19. #19
    Barefoot on the Moon! silver trophy Force Flow's Avatar
    Join Date
    Jul 2003
    Location
    Northeastern USA
    Posts
    4,606
    Mentioned
    56 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by asp_funda View Post
    You have to define a const with class definition and its value can't be changed. Read-only on the other hand can be set in a method (its rw within the class) but is read-only when accessed outside the class. That would definitely bring down the usage of getters.
    Ok, in that case:

    PHP Code:
    class thing {
        private 
    $attribute null;

        function 
    __construct($attr){
             
    $this->attribute=$attry;
        }

        function 
    getAttribute(){
             return 
    $this->attribute;
         }
    }

    $newthing = new thing('something');
    echo 
    $newthing->getAttribute(); 
    That essentially allows you to set the variable once, and get it at any time.


    Quote Originally Posted by Hall of Famer View Post
    Good point. Also const does not accept arrays or objects, it only works for numbers, booleans and strings.
    You could use serialize/unserialize to shoehorn arrays and objects into a const.
    Visit The Blog | Follow On Twitter
    301tool 1.1.5 - URL redirector & shortener (PHP/MySQL)
    Can be hosted on and utilize your own domain

  20. #20
    SitePoint Addict bronze trophy
    Join Date
    Apr 2013
    Location
    Ithaca
    Posts
    351
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by Force Flow View Post
    You could use serialize/unserialize to shoehorn arrays and objects into a const.
    Umm this... I thought PHP class constants must be declared and defined upon you write the class? Is it even possible to do this?

    PHP Code:
    class MyClass{
        const 
    constObject;

        public function 
    __construct(Object $object){
            
    self::constObject serialize($object);
        }

    Otherwise, you will have to actually know the serialized string representation for the array or object, not a good idea I guess...

  21. #21
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    Ok, in that case:

    PHP Code:
    class thing {
        private 
    $attribute null;

        function 
    __construct($attr){
             
    $this->attribute=$attry;
        }

        function 
    getAttribute(){
             return 
    $this->attribute;
         }
    }

    $newthing = new thing('something');
    echo 
    $newthing->getAttribute(); 
    That essentially allows you to set the variable once, and get it at any time.
    Or you could use __get() magic method do this:

    PHP Code:
    class thing {
        private 
    $attribute;
        private 
    $some_var 'pqr';

        public function 
    __construct($attr) {
             
    $this->attribute $attry;
        }

        public function 
    __get$var_name ) {
             if( isset( 
    $this->$var_name ) ) {
                 return 
    $this->$var_name;
             }
         }
    }

    $newthing = new thing'something' );
    echo 
    $newthing->attribute
    I didn't say its not possible currently, I said it would reduce the extra work of adding getter methods.

    Quote Originally Posted by Force Flow View Post
    You could use serialize/unserialize to shoehorn arrays and objects into a const.
    Yes but that's unnecessary extra work. I'd also like the ability to initialize class constants with value from a function output or for the ability to use final properties.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  22. #22
    SitePoint Guru
    Join Date
    Nov 2003
    Location
    Huntsville AL
    Posts
    689
    Mentioned
    4 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Force Flow View Post
    If you want a read-only variable, use const.
    And how do you go about having different const values for different instances? Answer: You don't. Think value objects?

    For getters/setters, you can use the magic methods __get() and __set(). Or, define your own functions. Or, make the variables public and access them through the object.

    PHP Code:
    class thing {
    public 
    $attribute=null;


    $newthing = new thing();
    $newthing->attribute='blue';
    echo 
    $newthing->attribute
    Don't want to. Just unnecessary clutter. Which is the point.

    If you want documentation to appear, use PHPdoc syntax, which can be parsed in most IDEs that supports PHP.

    PHP Code:
    /**
    * function description here
    * @param string $input some comments here
    * @return string some comments here
    */
    function testme($input){
    return 
    $input;

    Don't want to. Unnecessary clutter. I can almost always determine what the input values do by their names. Likewise, given the type I can usually determine the expected return value. No need to have extra annotations.

    I think some of this boils down to partly unfamiliarity with PHP
    At last something we can agree on!

    and partly wanting it to be something it is not.
    Which is sort of the point of having a wish list.
    Last edited by ahundiak; Aug 16, 2013 at 13:04. Reason: Made snarkness more subtle

  23. #23
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by pbyrne84 View Post
    It should be forever newbie friendly and not be chosen for bigger projects
    I've actually heard/read that a lot over the years (that PHP isn't for big/enterprise projects, blah blah blah) & unsurprisingly almost 99% times the people who've ranted about it are the ones who don't know jack about what they were talking. I guess Facebook, Yahoo, Flickr, Wikipedia etc just don't know what they've been doing for years! I just ignore such (not so) well-informed & opinionated people now.
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  24. #24
    SitePoint Wizard silver trophybronze trophy asp_funda's Avatar
    Join Date
    Jun 2003
    Location
    ether
    Posts
    4,497
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ahundiak View Post
    Which is sort of the point of having a wish list.
    Wishing for a speedboat to be able to fly above clouds would be classified as a fantasy!
    Our lives teach us who we are.
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    Me - Photo Blog - Personal Blog - Dev Blog
    iG:Syntax Hiliter -- Colourize your code in WordPress!!

  25. #25
    Hosting Team Leader silver trophybronze trophy
    cpradio's Avatar
    Join Date
    Jun 2002
    Location
    Ohio
    Posts
    5,126
    Mentioned
    152 Post(s)
    Tagged
    0 Thread(s)
    I carefully agree with you too @Hall of Famer ;

    Personally, I think the lack of return types cripples PHP's OOP opportunities on a very high level. You can't adequately utilize Interfaces without a bit of concern. If I could tell my Interface, the type it should return for each method, that would give me the comfort knowing that a class that implements that interface will return an expected type. Today, that isn't the case, it could implement the parameters of the method, but return a completely different object.

    My work is big on the need for interfaces (they are a big part of API design), DI can use them for further extensible opportunities too, but without return types, it makes it difficult to fully trust them.

    Read-Only can "kind of" be implemented now, just create a private variable in your object and a public getter method, but having a readonly attribute would be a "nice to have".

    For auto boxing/unboxing, I can see the usefulness, but at the same time, I think readability is sometimes better knowing you have to ask to do it.

    Having everything be an object would be nice, though I'm not sure it is absolutely necessary. Granted the biggest limitation it has right now is you can't extend a string since it isn't an object (you'd actually have to recreate it).


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
  •