PHP's type juggling, does it work with object's __toString() magic method?
Results 1 to 10 of 10

Thread: PHP's type juggling, does it work with object's __toString() magic method?

  1. #1
    Senior Member
    Join Date
    Sep 2011
    Posts
    258

    PHP's type juggling, does it work with object's __toString() magic method?

    Well in PHP, the below code defines number as string types but when you use plus, minus, multiply or other algorithmic operators they are converted to the corresponding number types:

    PHP Code:
    $byte "5";
    $double "6.4";
    $sum $byte $double;
    $product $byte $double;
    echo 
    $sum;
    echo 
    $product;
    // produces 11.4 and then 32. 
    Since PHP object has a magic method __toString(), I wonder if similar effect can be achieved with the following approach:
    PHP Code:
    class Integer{
       private 
    $value;

       public function(
    $value){
          
    $this->value = (int)$value;
       }

       public function 
    __toString(){
          return (string)
    $this->value;
       }
    }

    class 
    Double{
       private 
    $value;
       private 
    $precision;
       
       public function 
    __construct($value$precision ""){
          if(!empty(
    $precision)) $value = (int)($value pow(10$precision)) / pow(10$precision);
          
    $this->value $value;
          
    $this->precision $precision;
       }

       public function 
    __toString(){
          return (string)
    $this->value;
       }
    }

    $byte = new Integer(5);
    $double = new Double(6.4);
    $sum $byte $double;
    $product $byte $double;
    echo 
    $sum;
    echo 
    $product
    Will the code above produce the same result? Or will it generate a syntax error/exception? Just curious.
    Last edited by Lord Yggdrasill; 11-18-2012 at 10:23 PM.

  2. #2
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,844
    Will the code above produce the same result? Or will it generate a syntax error/exception? Just curious.
    Have you tried it?
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  3. #3
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by Weedpacket View Post
    Have you tried it?
    Bonus question: why does $sum end up holding the value 2?


    **********
    [edit]

    You could do it this way
    (but please, please, don't)
    PHP Code:
    <?php

    // your code . . .

    $sum = ((string)$byte) + ((string)$double);
    $product = ((string)$byte) * ((string)$double);
    Last edited by traq; 11-19-2012 at 10:52 PM.

  4. #4
    Senior Member
    Join Date
    Sep 2011
    Posts
    258
    Quote Originally Posted by traq View Post
    Bonus question: why does $sum end up holding the value 2?
    Is this what you got after trying out the code? Weird, thought it'd either throw an exception or work normally lol.

  5. #5
    Pedantic Curmudgeon Weedpacket's Avatar
    Join Date
    Aug 2002
    Location
    General Systems Vehicle "Thrilled To Be Here"
    Posts
    21,844
    An example of PHP's automatic type conversion is the addition operator '+'. If either operand is a float, then both operands are evaluated as floats, and the result will be a float. Otherwise, the operands will be interpreted as integers, and the result will also be an integer.
    language.types.type-juggling
    Caution: The behaviour of converting to integer is undefined for [objects]. Do not rely on any observed behaviour, as it can change without notice.
    language.types.integer.php#language.types.integer.casting
    THERE IS AS YET INSUFFICIENT DATA FOR A MEANINGFUL ANSWER
    FAQs! FAQs! FAQs! Most forums have them!
    Search - Debugging 101 - Collected Solutions - General Guidelines - Getting help at all

  6. #6
    Senior Member
    Join Date
    Sep 2011
    Posts
    258
    Thats too bad, I hope someday PHP just implements a __toPrimitive() method, which can be not only string, but also int, float, boolean or even null.

  7. #7
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    What you need to be doing instead is adding some math methods to you objects, or referencing the values directly:
    PHP Code:
    $sum $byte->theValue() + $double->theValue();
    // etc. 

  8. #8
    Senior Member
    Join Date
    Sep 2011
    Posts
    258
    Quote Originally Posted by traq View Post
    What you need to be doing instead is adding some math methods to you objects, or referencing the values directly:
    PHP Code:
    $sum $byte->theValue() + $double->theValue();
    // etc. 
    I do have a Math class with a group of static methods to do these, but I was wondering if there were easier ways. Anyway nvm, it is just an experience I am conducting, PHP clearly aint like ruby in which everything is an object so some techniques do not work.

  9. #9
    High Energy Magic Dept. NogDog's Avatar
    Join Date
    Aug 2006
    Location
    Ankh-Morpork
    Posts
    13,886
    I suppose that if you had the __toString() method return a numeric string, you could do something (kind of ugly) like:
    PHP Code:
    $total $someNumber + (string)$someObject
    But that seems a rather obfuscating thing to do -- not something I'd care to run into when maintaining that code a year later, even if I didn't have to prepend the (string) part. Much easier to read would be:
    PHP Code:
    $total $someNumber $someObject->descriptiveVariableName
    Your mileage may vary, of course.
    Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be." ~ from Nation, by Terry Pratchett

    "But the main reason that any programmer learning any new language thinks the new language is SO much better than the old one is because hes a better programmer now!" ~ http://www.oreillynet.com/ruby/blog/...ck_to_p_1.html


    eBookworm.us

  10. #10
    Senior Member traq's Avatar
    Join Date
    Jun 2011
    Location
    so.Cal
    Posts
    949
    Quote Originally Posted by traq View Post
    Bonus question: why does $sum end up holding the value 2?
    Quote Originally Posted by Lord Yggdrasill View Post
    Is this what you got after trying out the code? Weird, thought it'd either throw an exception or work normally lol.
    Did you try it out yourself?
    See if you can figure out why it gets that answer.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •