SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 49

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Design of higher-level classes

    I was reading some of Vincent (voostind)'s earlier posts on this forum, particularly this one. I thought I had an OK handle on OOD before, but that post in particular and his talk about putting different objects together to produce certain functionality makes such wonderful sense. I love it. However, all the OOD discussions I've looked through were focused on interaction of relatively low-level classes - iterators, database classes, et cetera. What I'm still quite confused about is how to handle the development of higher-level classes. I mean, at some point, classes have to start being more specific to the website or application that one is creating, right? I think an example would probably be most effective, although I'd be really interested in anyone's opinions on or experiences with this.

  2. #2
    SitePoint Addict mr tinkles's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    lots of ???s

    ...I mean, at some point, classes have to start being more specific..
    or, objects, but, yes they do, and they do.

    You appear to have an issue with 'scope' in OO. You can see the trees, but what's a forest? OR I understand nuts and bolts, but what's an erector set?

    Not a bad thing. Abstraction is not a fundamental of OO, but sure helps, and it may help here.

    If you can see how OO makes simple objects easier/more effecient/easy to handle, whatever. Then you need to see how OO applies to more complex objects!

    example:

    object sandwich
    class def{2 pieces of bread, stuff between}

    object bread
    class def{can be rye, wheat, white, muffin...}

    object mustard
    class def{yellar, poupon, etc...}

    object roastbeef
    class def{thin sliced...}

    using the defined objects, return me a sandwich object.

    if OO works great on simple things, apply same to complex things.

  3. #3
    midnight coder
    Join Date
    Dec 2000
    Location
    The flat edge of the world
    Posts
    838
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm guessing you're looking for some real life examples of how to put "different objects together to produce certain functionality"

    In one of my sites, I needed to make a search, when given a string, it searches different sections of the site, each section has a slightly different way of searching. eg a People section will be done by firstname/lastname, while an article section will be done by full text.

    PHP Code:
    class SearchString
    {
        function 
    search($searchString$searchObject)
        {
            
    //returns a MySQL result
            
    $result $searchObject->doSearch($searchString);
     
            
    $searchResults = array();
            for (
    $it =& new QueryIterator($result); $it->isValid(); $it->next()) {
                
    $row $it->getCurrent();
                
    $searchResults[$searchObject->getName($row)] = $searchObject->getValue($row);
            }
            return 
    $searchResults;
        }
    }
     
    class 
    SearchPeople
    {
        function 
    doSearch($searchString)
        {
            
    //returns result in a mysql result object
        
    }
     
        function 
    getName(&$row)
        {
            return 
    $row['last_name'].', '.$row['first_name'];
        }
     
        function 
    getValue(&$row)
        {
            return 
    $row['id'];
        }
    }
     
    class 
    SearchArticle
    {
        function 
    doSearch($searchString)
        {
            
    //returns result in a mysql result object
        
    }
     
        function 
    getName(&$row)
        {
            return 
    $row['title'];
        }
     
        function 
    getValue(&$row)
        {
            return 
    $row['id'];
        }

    Do do a search, you'll go:

    PHP Code:
    $s = new SearchString();
    $results $s->search('bob marley', new SearchPeople);
    foreach (
    $results as $name => $value) {
        echo 
    '<a href="people.php?id='.$id.'">'.$name.'</a><br />';

    Now you see, SearchString has become a lot more powerful, it has the ability to search different sections of the site by supplying it with a different search object, eg using SearchArticle instead.

    The objects by themselves are pretty useless, but by putting them together, they offer a range of functionaility
    Work smarter, not harder. -Scrooge McDuck

  4. #4
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks again mr tinkles. And Robo, it was helpful to see the same principles work at a higher level.

    I guess what I really mean is that in OOP, you try to make every class as generic as possible (solve a general problem rather than your specific situation), but at higher levels in the application, that's not possible (the class has to have functionality specific to your application... right?). I just find this a bit confusing, so I wanted to see an example of a high-level class.
    Last edited by ThreeDee; Mar 7, 2003 at 04:51.

  5. #5
    SitePoint Addict mr tinkles's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    so I wanted to see an example of a high-level class.
    that would be a car. if you can drive one, you can drive most. just give me the keys, I can most likely park the car.

    Are cars generic? On some levels, yes. Are they all the same, NO(abstract), are they OO, absolutely.

    abstract, generic, are the words that describe OO, they are not OO.

    When you go and buy a car, do the concepts of generic and abstract ever become part of the negotiations? The negaotiation is generally (another buzzword) about $$$ and car.

    Sorry, but this is how 'it' works...

  6. #6
    SitePoint Enthusiast
    Join Date
    Sep 2002
    Location
    Canada
    Posts
    27
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Good points, but then, that's the trouble with many OOP tutorials: they focus on these real-word metaphors and don't show you how they apply to programming. It would be nice to see an actual class that could be part of an actual website or PHP application...

  7. #7
    SitePoint Addict mr tinkles's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    they focus on these real-word metaphors and don't show you how they apply to programming.
    Sounds a little like mr tinkles, also, lol.

    It would be nice to see an actual class that could be part of an actual website or PHP application.
    I kind of think that's what these forums are about... As in:

    http://www.sitepointforums.com/showt...?postid=705790

    Here's something else:

    PHP taint great OO language, it's more kind of sort like OO.

    OOP is:

    Inheritance
    Polymorphism
    Encapsulation

    PHP has:

    Inheritance, sort of. One level only. More like 'include' or 'interface', barely inheritance. No function overloading/overriding!

    "PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions"

    That blows out Polymorphism, also!

    Encapsulation is how the code is implemented, so it has that as much as any structured programming.

    I personally haven't seen much need to objectize PHP any more than using functions, or as data structures (shopping cart?). Maybe the post will generate something interesting...

  8. #8
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    "PHP does not support function overloading, nor is it possible to undefine or redefine previously-declared functions"
    I presume that came from here: http://www.php.net/manual/en/functions.php

    Note that it says functions not class methods which can be over ridden in sub classes.

    As far as over loading class methods is concerned, this is not something you need to do in PHP, as a loosely typed language, compared to Java for example - you throw more arguments than was defined at a class method and catch them with func_get_args() and examine the type with gettype(). What's more, PHP has an extension called the overload extension which effectively allows it to be more powerful that Java in this area - I wrote an article about it here. Although experimental today, this is becoming one of the key elements of PHP5 and will give PHP a unique advantage, as an extremely flexible language.

  9. #9
    SitePoint Member Exp1Fusion's Avatar
    Join Date
    Mar 2003
    Location
    Atlanta, GA
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, some of the classes that you build won't be generic enough and will only be specific to your website. Unfortunately, you can't have it all even with the power of OO programming. As long as you don't have too much website specific code, then it's okay since it's almost impossible to generalize everything such that it can be reusable anywhere and everywhere.

    One of the rules of XP (extreme) programming is to just get the website working first. If you have to create code that will only work with that specific website configuration, then so be it. However, XP also recommends that later on in other iterations, that you can refactor your classes. So once you've got it working, you can then later on go back, if you wish, and make your class hiearchies, etc. less coupled and more refined.

    It's a waste of time in my opinion to try and always to create something so generic everytime unless you've got a lot of time to kill. However, that doesn't mean you shouldn't strive to create generic classes the first time either. Just remember there's disadvantages to either extreme, and that it's usually better to walk the middle ground. Additionally, another case for NOT choosing to create generic classes is that in general sometimes making something as generic as possible results in slower code, so depending on your situation, that may play a large factor in your designs.

  10. #10
    SitePoint Addict mr tinkles's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    aye carumba!

    PHP has all of these. It is perfectly possible to adhere completely to OOP principles, even in version 4.
    So does peanut butter and jelly. It is possible to adhere to OO principles with stuctured programming langs, such as BASiC and C, and FORTRAN. Doesn't make those langs OO...

    While PHP doesn't have overloading (which doesn't really have to do with inheritance anyway)
    inheritance can have a profound affect on overloading, so it's relevant. overloading/riding inheritance dependent.

    I don't know what you mean by one level of inheritance... maybe that dual inheritance isn't possible?
    No dual inheritance please, though possibly OO, more problems than worth. How come, now, you the expert?

    Do you even understand what overloading and overriding is, and more importantly what the difference is? I think that if you would, you wouldn't have made that statement because it has nothing to do with object oriented programming.
    I understand overloading/riding better than you, as neither exists without OO. The above quote will prove you wrong...

    What's more, PHP has an extension called the overload extension which effectively allows it to be more powerful that Java in this area - I wrote an article about it here. Although experimental today, this is becoming one of the key elements of PHP5 and will give PHP a unique advantage, as an extremely flexible language.
    The use of 'magic' functions to enable OO functionality (experimetal or not) proves my contention that PHP not OO compliant/capable! java does it WITHOUT overload/override KEYWORDs, hello. write that in your article, seems relevant...

    Kind of like this:

    I have a peanut butter and jelly sandwich.
    I call 'magic' function to change to pastrami and swiss cheese with that good mustard. ummm tasty.

    oops, guess .not does that stuff!

  11. #11
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well ignoring your not-so-hidden agenda to show PHP coders up for the charlatans they are for a moment and taking this at face value.

    So does peanut butter and jelly. It is possible to adhere to OO principles with stuctured programming langs, such as BASiC and C, and FORTRAN. Doesn't make those langs OO...
    And writing in C# or Java doesn't necessarily make you code great OO. How many people place all their code inside main()...

    The use of 'magic' functions to enable OO functionality (experimetal or not) proves my contention that PHP not OO compliant/capable! java does it WITHOUT overload/override KEYWORDs, hello. write that in your article, seems relevant...
    It think you'll find if you re read the article carefully, you'll find PHP is doing something which Java (and other strongly typed languages) don't have and cannot have.

    But let me make this clearer for you. Here's a Java class

    Code:
    class Point {
        int x;
        int y;
        public void draw (int x, int y) {
             this.x = x;
             this.y = y;
        }
        public void draw () {
             x = 0;
             y = 0;
        }
    }
    The method draw is overloaded. Here's how you do the same in PHP;

    PHP Code:
    <?php
    class Point {
        var 
    $x;
        var 
    $y;
        function 
    draw () {
            
    $numArgs func_num_args();
            
    $args func_get_args();
            switch ( 
    $numArgs ) {
                case 
    '2':
                    
    $this->x=$args[0];
                    
    $this->y=$args[1];
                    break;
                case 
    '0':
                    
    $this->x=0;
                    
    $this->y=0;
                    break;
            }
        }

    }
    ?>
    With a loosely typed language signatures are less important that they are with strongly typed - you'd do effectively the same with Python for example. If you're arguing the loosely typed languages are fundamentally not object oriented you'll have to take that all the PHP, Perl, Ruby and Python programmers who are quite happily writing OOP code, blissfully ignorant of the fact that is just isn't.

    What PHP's overload extension does is something unique, that you cannot do with Java or any other stronly typed language (without some major workarounds). But hey - prove me wrong if you can;

    PHP Code:

    class SomeClass {
        function 
    __call($method,$params,&$result) {
            
    $numArgs=count($params);
            switch ( 
    $numArgs ) {
                case 
    '3':
                    
    $result=$method($params[0],$params[1],$params[2]);
                    break;
                case 
    '2':
                    
    $result=$method($params[0],$params[1]);
                    break;
                case 
    '1':
                    
    $result=$method($params[0]);
                    break;
                default:
                    
    $result=$method();
                    break;
            }
            return 
    true;
        }
    }

    overload ("SomeClass");

    $someClass = new SomeClass;

    $someClass->str_replace('not OOP','very OOP','PHP is not OOP'); 
    I can now call anything via someclass. While you dismiss this you fail to see it's value in things such as web services and otherwise, where PHP interacts with other systems. This is not overloading in the sense of Java or other strongly typed languages. This is something unique to loosely typed languages.

    In my opinion, strongly typed languages are stuggling to keep up with loosely typed languages. One whole issue that is only occassionally discussed in todays web services hype is how XML schema was used to give strongly typed languages a change. An interesting footnote is MIME rpc: http://www.mime-rpc.com/ - the whole web services "thing" could have been a whole load easier - MIME rpc is perfect for loosely typed languages. As it is the only way to develop at an acceptable speed these days with a strongly typed language is to take a language like C# and super charge with tools (which suits the tool vendor very nicely).

    Where I agree with you, in something you haven't actually said yet, is that today, with PHP 4, the programming "experience" is not all it should be. It needs to standardise things which currently every PHP developer has their own solution for, such as exception handling, static members, abstract classes and so on. That's exactly why the lead PHP developers are entirely focused on PHP 5 right now. You should read more at http://www.phpvolcano.com/eide/php5.php?page=front for a nice intro.

    Of course I'm the blind teaching the blind...
    Last edited by HarryF; Mar 10, 2003 at 00:01.

  12. #12
    SitePoint Addict -TheDarkEye-'s Avatar
    Join Date
    Feb 2001
    Location
    canada
    Posts
    286
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    this may not be something that all c++ programmers are aware of, and also im not sure if you can do this with java, but...

    http://www.function-pointer.org/CCPP...m_fpt.html#why

    ...is a good example that proves you partially wrong harry. as for the function arguments there are relatively simple ways to get around that as well.

    btw harry, i didnt like your example very much. im not sure if this is possible in php (i know it is in other scripting languages), but what if you want to have n arguments instead of 0, 1, 2, or 3?

  13. #13
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Inheritance, sort of. One level only. More like 'include' or 'interface', barely inheritance.
    now if even a OOP shovel like me knows you are talking out of your nether regions there I suggest you get to grips with PHP OOP before regurgitating shovel-bait about what it can and can not do.

    TheDarkEye , I am not quite with you but I thought that Harry's code showed now $n variable arguments were handled? - if not just take a look at the function handling functions at php.net especially variable functions and create_function() which is immensly useful , especially in callbacks in php-gtk where I can create functions based on user input.

    I dont think it gets much more OOP than a GUI , but as I say I am an OOP shovel In fact the only thing that really hurts me in my day to day meanderings is lack of decent dereferencing which is happily now being rectified (and lack of a basic OOP grounding which means I keep reinventing the wheel ..& at a penalty of course )

  14. #14
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Holy! Nice one Firepages. For some reason I've never noticed create_function() - from a glance looks like there's a way there to use call for a function with any number of arguments. More later...

    PS: on the PHP OOP front - how about a UML tool which generates PHP? http://uml.sourceforge.net

  15. #15
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by HarryF
    PS: on the PHP OOP front - how about a UML tool which generates PHP? http://uml.sourceforge.net

    ohhhh very nice till I saw the KDE bit grrrrrrrrrr

  16. #16
    SitePoint Wizard gold trophysilver trophy
    Join Date
    Nov 2000
    Location
    Switzerland
    Posts
    2,479
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might want to keep an eye of phpedit. If you have a UML tool capable of generating XMI files - I believe Visio will do this or alternatively ArgoUML that phpEdit has an XMI2PHP tool in progress.

  17. #17
    SitePoint Addict mr tinkles's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well ignoring your not-so-hidden agenda to show PHP coders up for the charlatans they are for a moment and taking this at face value.
    Guess I deserve that. If any agenda is hidden, it's to perhaps understand better/more how PHP does OO, make all better coders.

    And writing in C# or Java doesn't necessarily make you code great OO. How many people place all their code inside main()...
    OR worse, write horrible OO, because they can, or don't know any better...

    It think you'll find if you re read the article carefully, you'll find PHP is doing something which Java (and other strongly typed languages) don't have and cannot have.
    I read and understood the article. I'm fairly sure I got it, though correct me wrong.

    I can now call anything via someclass. While you dismiss this you fail to see it's value in things such as web services and otherwise, where PHP interacts with other systems. This is not overloading in the sense of Java or other strongly typed languages. This is something unique to loosely typed languages.
    I quess this is where you and I disagree. It's kind of overloaded, as in:

    $point->draw(5,9);
    $point->draw();

    You can overload on number of args, could be useful, could be dangerous. What results do you get with:

    $point->draw("some url",$array);
    $point->draw($array, "some url");

    Truly overloaded is by method signature, not number of args

    function (int){}
    function (string){}
    function (object){}
    function (int,string,obj){}

    Now, I know PHP no do this (or please show me), thats OK, just don't claim it does.

    Your example of overloading can be easily accomplished in Powerbuilder (been awhile) by use of the ANY variable type and still supports datatyping. Would seem reasonable to me that you could do the same in Java, though I haven't tried.

    PB:

    return functionname(any, any, any, any, any...N)

    You have now overloaded functionname for up to N args, just call:

    functionname(int, string, long) OR
    functioname (string)

    you of course still need to code for all the combo of args you want to use, much like you example, but with datatyping (or NOT)

    java:

    public void method (Object x, Object y, Object z,... Object N)
    {
    //ton of code for combos of args
    }

    You have now overloaded functionname for up to N args, just call:

    method (string, int, string, null, null, null ...N)
    method (string, string, null, null, null ...N)

    Think that would work, though not very elegant.

    Captain Proton, that's overloading...

    Where I agree with you, in something you haven't actually said yet, is that today, with PHP 4, the programming "experience" is not all it should be.
    Well, 4 much better than 3, maybe 5 even better??? We'll see.

    now if even a OOP shovel like me knows you are talking out of your nether regions there I suggest you get to grips with PHP OOP before regurgitating shovel-bait about what it can and can not do.
    So what are you trying to say? How about splaining this Lucy!

    <?php
    class a
    {
    function a()
    {
    echo "<p>object a constructor";
    //this call should create infinite loop
    echo $this->a();
    //$this->temp doesn't exist
    echo "<p>temp=".$this->temp;
    }
    }

    class b extends a
    {
    var $temp;

    function b()
    {
    echo "<p>object b constructor";
    $this->temp=1;
    echo "<p>temp=".$this->temp;
    parent::a();
    }
    }

    class c extends b
    {
    var $temp;
    function a()
    {
    echo "<p>function a on object c";
    }

    function c()
    {
    echo "<p>object c constructor";
    $this->temp=2;
    echo "<p>temp=".$this->temp;
    parent::b();
    echo $this->a();
    //why is this not 2?
    echo "<p>temp=".$this->temp;
    }
    }
    print "<p> create a c object\n";
    $nc = new c;
    ?>
    That is god awful code, OO, or otherwise, just for example purposes...

    Seems PHP kinda loses "this" references to me, when inheriting more than 1 level. Of course if only inherit one level, not an issue. You can try this by instantiating a b object, but the $this->a(); creates infinite loop! But not with 2 levels. Seems I have a grip, what about you?

  18. #18
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    //why is this not 2?
    echo "<p>temp=".$this->temp;
    parent::b .... which is doing exactly what you tell it to do ? what else would you expect to happen ?

    if you write code that is deliberately flawed then what else can it produce ? , if we are daft enough to put $this->a() in the base class a constructor then we have to be sure that it is overridden less we expect the results above.

  19. #19
    SitePoint Addict mr tinkles's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    if you write code that is deliberately flawed then what else can it produce ? , if we are daft enough to put $this->a() in the base class a constructor then we have to be sure that it is overridden less we expect the results above.
    how is it 'deliberately flawed', exactly?

    what? does it work?

    if you are allowed to write bad code, it will happen.

  20. #20
    ********* wombat firepages's Avatar
    Join Date
    Jul 2000
    Location
    Perth Australia
    Posts
    1,717
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no, if you write bad code it will happen, you can write bad code in any language.

    again , I am not an OOP head but under what circumstances would you ever have this... ?

    class foo{
    function foo(){
    //gonna loop unless foo is overridden elsewhere
    //so why would you ?
    $this->foo();
    }
    }

    If there is a reason, then OK , I just don't see/understand it.

    its flawed (from my perspective) in that you basically say
    $temp=2;//then call b() so
    $temp=1;
    echo $temp;
    //why does temp not ==2 ?

    at least thats what your code does and returns exacly what it has been created to do.

    If your argument is that its easy to write bad code with PHP especially OOP then no-one will argue, but you are saying that PHP OOP is not capable as is and thats simply not true.

    That PHP has until now required workarounds to create some OOP features , private variables, abstract classes etc too is accepted, again that does not limit your possibilities, in the same way as PHP not having application scope globals & variables does not stop one from easily achieving such.

  21. #21
    SitePoint Guru
    Join Date
    Oct 2001
    Posts
    656
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mr tinkles
    Captain Proton, that's overloading...
    I knew that, but thanks for reminding me

    Still, overloading is not a concept that is fundamental to OOP.

  22. #22
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Captain Proton
    Still, overloading is not a concept that is fundamental to OOP.
    And I'd like to add - if my words still account for anything after the MVC debacle of last week - this is absolutely correct.

    Overloading has NOTHING to do with OOP. Overriding does, but overloading doesn't.

    You can do this in Java:
    Code:
    public class Car { }
    public class Ferrari extends Car {}
    public void drive(Car c) { };
    drive(new Ferrari());
    Here, a Ferrari object is passed to a method that accepts a Car object. But that's not overloading, because a Ferrari IS a Car. The method 'drive' expects a Car object, and gets a Car object. No overloading involved whatsoever. Polymorphism yes. Overloading no.

    By the way, Mr. Tinkles wrote this piece of Java:
    Code:
    public void method (Object x, Object y, Object z,... Object N) 
     {
     //ton of code for combos of args
     }
    
    method (string, int, string, null, null, null ...N)
    This doesn't work, because there is no 'string' type in Java, and the 'int' type is not of type Object. If he would have written:

    Code:
    method (String, Integer, ...N);
    Then it would have worked, because both class String and class Integer are subclasses of class Object. Again, this is not overloading, but polymorphism. Anyway, what would be the use of doing this? The method 'method' accepts a number of Object instances, so it can only call methods defined in that particular class, and not any method defined somewhere else (it's Java, remember!). This is where PHP differs. Because in PHP you can do this:

    PHP Code:
    function method($object) {
        
    $object->drive();

    The method 'method' above calls the method 'drive' on an object '$object'. Why is that so special? Because nowhere is it defined that the variable '$object', or the class it is an instance of, must support the method 'drive'. Also, if two objects happen to support the method drive(), that doesn't mean they have to be related in any way through inheritance. That's something you can only try to approach with Java using interfaces, but it's nowhere near as flexible as PHP. Or Ruby. Or Python. Or any other object oriented language with dynamic late binding... (Smalltalk anyone?)

    Vincent

  23. #23
    SitePoint Addict mr tinkles's Avatar
    Join Date
    Jan 2003
    Posts
    262
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    If there is a reason, then OK , I just don't see/understand it.
    The reason is simple:

    "That is god awful code, OO, or otherwise, just for example purposes..."

    The example being, poor self reference, and poor variable shadowing. Would a better example, that shows the same behavior be any better? The code a silly as it is, is actually very possible in the real world when one wants to extend existing objects that require class constructors???

    its flawed (from my perspective) in that you basically say...
    $temp exists in both b and c. If I am a c object, and I set my $temp=2, then:

    echo $temp;

    should output 2 regardless of what any ancestors do. If $temp is a class level variable, then $temp should exist in both the c and b object as their own class vars. If $temp class var across inheritance, then overriding big problem...

    Overloading has NOTHING to do with OOP. Overriding does, but overloading doesn't.
    Polymorphism does, and doesn't number 2 look like overloading?

    http://searchsolaris.techtarget.com/...212803,00.html

    What do you think OOP means?

    This doesn't work, because there is no 'string' type in Java, and the 'int' type is not of type Object. If he would have written:...
    you are correct, but was it so hard to get the idea, as in you know what I meant, or maybe not?

    Here, a Ferrari object is passed to a method that accepts a Car object. But that's not overloading, because a Ferrari IS a Car. The method 'drive' expects a Car object, and gets a Car object. No overloading involved whatsoever. Polymorphism yes. Overloading no.
    See the above definition of polymorphism, means having "having multiple forms", NOT multiple names. The example has little or nothing to do with polymorphism.

    Though it does show a potential liability:

    public class Car { }
    public class Ferrari extends Car {}
    public void drive(Car c) { };
    drive(new Ferrari());

    As defined, may not work reliably. True, all Ferrari's are Car's, but not all Car's are Ferrari's. If drive() method written for Car, and doesn't take potential for Ferrari object, then implicit cast will occur and you will lose Ferrari functionality in drive() method and get just Car (will need to cast back to Ferrari object).

    If you want a Ferrari and want to drive, but get yugo, would you be happy?

    Not a good OO example. Ferrari different than Car, though is car. Do you take your Ferrari to a Car mechanic?

    Lastly:

    The method 'method' above calls the method 'drive' on an object '$object'. Why is that so special? Because nowhere is it defined that the variable '$object', or the class it is an instance of, must support the method 'drive'.
    That's a good thing? And when an object that doesn't have drive() is passed, what now?

    I can call unknown methods on unknown objects, how it that good?

    [humor]
    loosely typed lips, sink ships.
    [/humor]

  24. #24
    SitePoint Evangelist
    Join Date
    Oct 2001
    Posts
    592
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mr tinkles
    Polymorphism does
    Of course polymorphism has everything to do with OOP. I never said otherwise. It was you who said that overloading had everything to do wich OOP, which is incorrect. You mix up the various definitions of polymorphism in the link you posted (overloading is a kind of polymorphism according to that definition, but that kind of polymorphism had nothing to do with OOP), and I only pointed that out.

    Quote Originally Posted by mr tinkles
    What do you think OOP means?
    You're asking me? Want my resume? And while we're at it, may I see yours?

    The person who tells others that they don't know enough about some subject, is surprisingly often the one who knows the least about that subject...

    Quote Originally Posted by mr tinkles
    See the above definition of polymorphism, means having "having multiple forms", NOT multiple names. The example has little or nothing to do with polymorphism.
    The example has everything to do with polymorphism. A Ferrari (in my example) is another 'form' of a car; not another name.

    Quote Originally Posted by mr tinkles
    As defined, may not work reliably. True, all Ferrari's are Car's, but not all Car's are Ferrari's. If drive() method written for Car, and doesn't take potential for Ferrari object, then implicit cast will occur and you will lose Ferrari functionality in drive() method and get just Car (will need to cast back to Ferrari object).
    Correct. And this is precisely the problem with your so-called overloading example 'method (Object o1, Object o2, ...N)'. Again, I only pointed that out using this example. The only one you're arguing with here is yourself.

    Quote Originally Posted by mr tinkles
    Not a good OO example. Ferrari different than Car, though is car. Do you take your Ferrari to a Car mechanic?
    Why not? A ferrari is a 'form' of a car (in your speak), so it supports everything a normal car does. So if it doesn't work because of a simple 'car' problem, any Car mechanic can fix it. That's the whole point of polymorphism. The Ferrari -> Car (Or Student -> Person) example is a famous (or infamous) one. And now you're saying it's a bad example? That's a good one!

    Quote Originally Posted by mr tinkles
    That's a good thing? And when an object that doesn't have drive() is passed, what now? I can call unknown methods on unknown objects, how it that good?
    It's not good OR bad. It's different. It allows you to do either very stupid things, or very great things, depending on your knowledge and experience. And it's totally different from Java (or C++).

    Vincent

  25. #25
    As the name suggests... trickie's Avatar
    Join Date
    Jul 2002
    Location
    Melbourne, Australia
    Posts
    678
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Mr Tinkles, i think you should re-asses your 'arguments' and who's skills you are questioning.

    If u write badly designed code, you will get a bad result. Do you need to have good programming practices enforced upon you, or can you take responsibility?


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
  •