Is this a Normal PHP Coding practice?



  • Most of the programming I do is in strongly typed languages, so maybe this isn't even a WTF, but I find this code example very odd:

    Is it normal to have to have to add 0 to a number to cast it to an integer for PHP interop with Java?  If so, is that a good thing?

     



  • AFAIK, better practice would have been to do:

    $javaObject->doSomething((int)$var);

    But, even that has issues, such as "What if that function handles variables by reference?"
    Better practice would be to typecast the variables within the map from PHP to Java.


  • I'm not sure why you think this is a wtf.

    You sent Java (which is strongly typed language with overloads, btw) [code]doSomething(string)[/code].  There is no [code]public void doSomething(string i)[/code]

    Therefore, you get an error from Java saying that the method doesn't exist, stupid!



  • @Valarnin said:

    AFAIK, better practice would have been to do:

    $javaObject->doSomething((int)$var);

    But, even that has issues, such as "What if that function handles variables by reference?"
    Better practice would be to typecast the variables within the map from PHP to Java.

    Java doesn't support pass by reference.  Even objects are passed by value (but that value is a reference).  Oh, and I doubt the Java Bridge allows you to pass objects between languages like this, at least not without some sort of mapping taking place.

    PHP doesn't support pass by reference unless it's declared on both sides.




  • @powerlord said:

    I'm not sure why you think this is a wtf.

    You sent Java (which is strongly typed language with overloads, btw) <font face="Lucida Console" size="2">doSomething(string)</font>.  There is no <font face="Lucida Console" size="2">public void doSomething(string i)</font>

    Therefore, you get an error from Java saying that the method doesn't exist, stupid!



    I'm not sure it's what he was directly referring to as a WTF, but the adding 0 to a string to cast it to an integer is definitely a WTF.



  • @Valarnin said:

    @powerlord said:

    I'm not sure why you think this is a wtf.

    You sent Java (which is strongly typed language with overloads, btw) <font face="Lucida Console" size="2">doSomething(string)</font>.  There is no <font face="Lucida Console" size="2">public void doSomething(string i)</font>

    Therefore, you get an error from Java saying that the method doesn't exist, stupid!



    I'm not sure it's what he was directly referring to as a WTF, but the adding 0 to a string to cast it to an integer is definitely a WTF.
     

    PHP is a weakly typed language and + is a numeric operator only in PHP (String Concatenation is [code].[/code]).  Using a + converts both operators to numbers first.  And yes, this [url=http://www.php.net/manual/en/language.types.type-juggling.php]is documented[/url].



  •  @frits said:

    Is it normal to have to have to add 0 to a number to cast it to an integer for PHP interop with Java?  If so, is that a good thing?
    That seems fairly typical to me, as far as PHP is concerned.  Read the official PHP documentation on the subject - that's pretty much the preferred method of casting a string to an integer, and I've done similar things myself.  You can explicitly cast a PHP variable to a specific type, but there are a number of different ways of doing it depending on what your end goal is.



  • @powerlord said:

    I'm not sure why you think this is a wtf.

    You sent Java (which is strongly typed language with overloads, btw) <FONT face="Lucida Console" size=2>doSomething(string)</FONT>.  There is no <FONT face="Lucida Console" size=2>public void doSomething(string i)</FONT>

    Therefore, you get an error from Java saying that the method doesn't exist, stupid!

    Wow, it's usually considered bad form to call someone stupid when you have low reading comprehension yourself.  I didn't write the code, so [i]I[/i] didn't send anything to Java. 

    So you are saying the example is a WTF?



  • I believe that sample may predate the addition of the casting operator to PHP. After all, it's referring to Java 1.4 which was released in 2002, and explicit casting was first implemented in PHP around the same time IIRC.



  • @powerlord said:

    PHP doesn't support pass by reference unless it's declared on both sides.
     

    Not exactly.  In PHP4, everything was passed by value by default, and pass-by-reference could be declared either in the function call or in the function signature, but didn't have to be declared by both.  In PHP5, objects are passed by reference by default; primitives and arrays are passed by value by default.  Pass by reference for primitives and arrays is now declared in the function signature -- after 5.3, declaring it in the function call raises a noisy deprecation warning.



  • out of topic:

    The_assimilator, very good choice of music 😃



  • @The_Assimilator said:



    I believe that sample may predate the addition of the casting operator to PHP. After all, it's referring to Java 1.4 which was released in 2002, and explicit casting was first implemented in PHP around the same time IIRC.
    I doubt it. Elsewhere in the article, it says that their bridge will always claim that it's 2008 somewhere in the USA. I think it's much more likely that the instructions were written in 2008 somewhere in the USA.

    An explicit cast would make more sense to me, as it would clearly show that you don't intend to call doSomething(float i) instead.


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.