SitePoint Sponsor

User Tag List

Results 1 to 21 of 21
  1. #1
    SitePoint Enthusiast csjc2662's Avatar
    Join Date
    Feb 2007
    Location
    Texas, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Operator Overloading

    Hello,
    I found this old thread by searching http://www.sitepoint.com/forums/show...or+overloading and was wondering if anyone still used the http://pecl.php.net/package/operator package or if there is a better one or better way to do it now.

    If not what has been your experiance with this package or anyother you could suggest. I have just recently moved to PHP form C++ and find some parts of its OOP concepts and support quite different. Like having to declare each member or method's visibility instead of with one declaration and then listing them under it.

    Thanks,
    James
    James
    New links coming soon....

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    170
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by csjc2662 View Post
    Like having to declare each member or method's visibility instead of with one declaration and then listing them under it.
    You can declare variables sharing the same visibility by stating one keyword and separating them with a comma:
    PHP Code:
    private $foo$bar 'foo'$foobar
    Same goes for static class variables and static local variables. It doesn't apply to method declarations however.
    Last edited by michel; May 6, 2007 at 11:19.

  3. #3
    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)
    And you don't have to explicitly declare a method public -- it's implied.

  4. #4
    SitePoint Enthusiast csjc2662's Avatar
    Join Date
    Feb 2007
    Location
    Texas, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yes, I am not asking about that though. I am asking about operator overloading as mentioned in the tread I linked to above it is just over a year old so I am asking if there is anything better or a new way of doing that. I understand visibility and how it works in php I just said it is one of php's traits that is a little bit tedious. I am interested in operator overloading.
    Such as <<, ==, !=, =, +, -, etc.
    James
    New links coming soon....

  5. #5
    SitePoint Enthusiast
    Join Date
    Mar 2005
    Posts
    94
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The PECL extension is the only way to do operator overloading in PHP. As you
    see the extension is beta and its likely that its never getting stable and
    patching PHP for full support is a little bit stupid.

    Well, oo has its applications but i suggest that you get into PHP without making
    a new C++ out of it.

  6. #6
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The only operator you can overload in php is [] (via offsetGet/set) and I think it's good so. Operator overloading is perhaps the quickest way to shoot yourself in the foot.

  7. #7
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by stereofrog View Post
    Operator overloading is perhaps the quickest way to shoot yourself in the foot.
    It's also the nicest way to add basic data types, such as vectors and matrices, to the language. Though, they aren't used much in the PHP world, so having operator overloading wouldn't make much sense.

    Another option might be a DSL, also hardly used in our community, but using operator overloading for that just seems ugly and it'd be better to have something like LISP's macros.

  8. #8
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jasper Bekkers View Post
    It's also the nicest way to add basic data types, such as vectors and matrices, to the language.
    I don't want to start anything here, but I honestly fail to see the advantage of operator overloading in such cases.

    Compare these two:
    PHP Code:
    $v1 $v2 $v3;
    $v1 $v2->cross($v3); 
    IMO there is hardly any difference between the two, besides aesthetics (in favour of the first example) and code clarity (in favour of the second). Is it worth the fuss? Is there a "killer example" for operator overloading that's not just a prettier method call?

  9. #9
    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 dbevfat View Post
    Is there a "killer example" for operator overloading that's not just a prettier method call?
    There can't be, because that's all that an operator is -- a method call with a different syntax. Just ask the Smalltalk guys.

  10. #10
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac View Post
    There can't be, because that's all that an operator is -- a method call with a different syntax. Just ask the Smalltalk guys.
    A C++ guy will tell you the same. But it does look more fluent for mathematical expressions. Overloading the * operator on a vector to do a cross product might be a little vague because it could also imply a dot product which would be less readable.

  11. #11
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by BerislavLopac View Post
    There can't be, because that's all that an operator is -- a method call with a different syntax. Just ask the Smalltalk guys.
    Indeed, in OOP terms "A+B" means nothing more than "send A a message '+' with argument B". However, operators have a big advantage of brevity. While overloading is ugly, the ability to define own operators would be great for creating DSLs.
    Code:
    Merchant pays_to Supplier 10 percent_of TotalCost
    where pays_to and percent_of are own operators.

  12. #12
    SitePoint Enthusiast csjc2662's Avatar
    Join Date
    Feb 2007
    Location
    Texas, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the great input from all.
    Quote Originally Posted by stereofrog View Post
    Operator overloading is perhaps the quickest way to shoot yourself in the foot.
    Yes it is and so is multiple inheritance but when used correctly can be a valuable resource. Especially in cross platform programming. It allows for a greater degree of abstraction. And yes that is not something that is often required when using PHP.

    Quote Originally Posted by Jasper Bekkers View Post
    It's also the nicest way to add basic data types, such as vectors and matrices, to the language. Though, they aren't used much in the PHP world, so having operator overloading wouldn't make much sense.
    Well matrices are used fairly often in code I have read from others that is simply a multidimensional array, also known as associative array(2 dimensional) just with a different view of the semantics.

    The reason I asked is because the application I'm currently writing will also involve a lot of mathematical calculations and functions to handle sales stats and sales organized by several possible categories. As well as the usual cms style functionality. It would help tremendously in that context when relating to time spent typing all the method calls and such.
    James
    New links coming soon....

  13. #13
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by csjc2662 View Post
    Well matrices are used fairly often in code I have read from others that is simply a multidimensional array, also known as associative array(2 dimensional) just with a different view of the semantics.
    I know what a multidimensional array is, thank you. I don't see, however, how it would make sense to overload mathematical operators on an arbitrary (multidimensional) array. Matrices, at least in 'my' field, are used extensively for transformations in 2- or 3 dimensions though Affine transformations; in which case it makes sense to have a separate Matrix class with the corresponding operators.
    The reason I asked is because the application I'm currently writing will also involve a lot of mathematical calculations and functions to handle sales stats and sales organized by several possible categories. [...] It would help tremendously in that context when relating to time spent typing all the method calls and such.
    I would look into Domain Specific Languages too before turning my head to an uncommon PECL extension; they might not survive in newer revisions of the Zend engine.

  14. #14
    SitePoint Enthusiast csjc2662's Avatar
    Join Date
    Feb 2007
    Location
    Texas, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I have no intention to use the extension as it is not quite what I was looking for. And I meant no offense. And yes using the multidimensional arrays for the math I'm talking about would be useful. It is for a stats, commission, venue, event, tickets, sales, fees, per user, order, mean order totals, etc. And all to be able to be viewed in a admin control panel. As well as sort by date and be able to print out the stats to show potential clients/venues the statistics for the site/promoter using almost any category to sort by or show that areas stats. A challenge yes/no/maybe but I was looking to see if there was any other ways to do operator overloading. If not I can work around it. I'm not a newbie to programming or OOP just new to PHP and its OOP implementation.
    James
    New links coming soon....

  15. #15
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by csjc2662 View Post
    I have no intention to use the extension as it is not quite what I was looking for. And I meant no offense.
    Forgive me, I was under the impression you were. You didn't offend me in any way, I just might sound harsh sometimes; English isn't my native language.
    And yes using the multidimensional arrays for the math I'm talking about would be useful. It is for a stats, commission, venue, event, tickets, sales, fees, per user, order, mean order totals, etc.
    I'd hardly overload operators on those entities; they're not mathematical concepts. Yeah, they're scalars, for which the operators are already defined in PHP itself. What you're looking for is something to handle your business rules, something that makes the implementation easier and more maintainable. Operator overloading is not going to help you with that. Of course I don't know any specifics but I'd rethink the thing and try a solution different than what you're used to.
    If not I can work around it. I'm not a newbie to programming or OOP just new to PHP and its OOP implementation.
    If you have any previous experience with Java try to exploit that, C++ vastly different for a large variety of reasons.

  16. #16
    SitePoint Enthusiast csjc2662's Avatar
    Join Date
    Feb 2007
    Location
    Texas, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jasper Bekkers View Post
    I'd hardly overload operators on those entities; they're not mathematical concepts. Yeah, they're scalars, for which the operators are already defined in PHP itself. What you're looking for is something to handle your business rules, something that makes the implementation easier and more maintainable. Operator overloading is not going to help you with that. Of course I don't know any specifics but I'd rethink the thing and try a solution different than what you're used to.

    If you have any previous experience with Java try to exploit that, C++ vastly different for a large variety of reasons.
    Well the operator overloading is to make less typing while building the formulas and working with the data as I plan on putting it in a array to handle it better than multiple query's for load time sake(javascript display functions. I guess that is called AJAX now.) as well as possible server load because the site that is related to the one i am programming and on the same server is already heavily used and has a fairly large number of users. And I am trying to migrate from C++ so that is where a lot of my mind set is from. I'm trying to work on that though.
    James
    New links coming soon....

  17. #17
    SitePoint Addict Jasper Bekkers's Avatar
    Join Date
    May 2007
    Location
    The Netherlands
    Posts
    282
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by csjc2662 View Post
    Well the operator overloading is to make less typing while building the formulas and working with the data
    Operator overloading should be used for clarity, to make code more readable, if you want less typing consider using the C preprocessor or Perl.

  18. #18
    SitePoint Enthusiast csjc2662's Avatar
    Join Date
    Feb 2007
    Location
    Texas, USA
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    well the overloaded operators would make the formulas much more clear from a mathematic perspective by clarifiying the order of operations but that is not the point either. I have to write 1000 lines of code and i can shorten it by about say 5 characters per line that equals 5000 key presses and that adds up and leaves a concise formula to be inspected even if the overloaded operators are encapsulated in a object for internal use only.... That is just my opinion though. And yes I don't have the most comfortable typing arrangement so that also plays a factor in the shorter code if possible without loosing readability.
    James
    New links coming soon....

  19. #19
    SitePoint Wizard stereofrog's Avatar
    Join Date
    Apr 2004
    Location
    germany
    Posts
    4,324
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by csjc2662 View Post
    Well the operator overloading is to make less typing while building the formulas and working with the data
    You're going to love J then

    Code:
    # quicksort function in J
    
    quicksort=: (($:@(<#[) , (=#[) , $:@(>#[)) ({~ ?@#)) ^: (1<#)

  20. #20
    Non-Member
    Join Date
    Jan 2003
    Posts
    5,748
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    quicksort=: (($:@(<#[) , (=#[) , $:@(>#[)) ({~ ?@#)) ^: (1<#)
    Huh? Wtf?!

  21. #21
    SitePoint Guru dagfinn's Avatar
    Join Date
    Jan 2004
    Location
    Oslo, Norway
    Posts
    894
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Wink

    Or, at the other extreme, there's Perl in Latin:
    Code:
    #! /usr/local/bin/perl -w
    
            use Lingua::Romana::Perligata;
    
            maximum inquementum tum biguttam egresso scribe.
            meo maximo vestibulo perlegamentum da.
            da duo tum maximum conscribementa meis listis.
    
            dum listis decapitamentum damentum nexto
                fac sic
                    nextum tum novumversum scribe egresso.
                    lista sic hoc recidementum nextum cis vannementa da listis.
                cis.
    Dagfinn Reiersøl
    PHP in Action / Blog / Twitter
    "Making the impossible possible, the possible easy,
    and the easy elegant"
    -- Moshe Feldenkrais


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
  •