type hint from php class library?

Discussion about the open-source Phalanger [?] project.

type hint from php class library?

Postby noname0x100 » May 7th, 2012, 7:55 pm

I have some methods in a PHP Class Library. If im called this methods in my C# application, im must already do "ToString" or other else. The methods return string, integer, boolean, much more. How can i fix this "bug"?
noname0x100
 
Posts: 13
Joined: March 17th, 2012, 12:37 pm

Re: type hint from php class library?

Postby Jakub Misek » May 7th, 2012, 8:11 pm

Do you have an example?
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague

Re: type hint from php class library?

Postby noname0x100 » May 8th, 2012, 3:54 pm

PHP Library Class:
Code: Select all
namespace TEST {
  class Example {
    public static function myMethod () {
      return 'Hello World!';
    }
  }
}


C# Application:
Code: Select all
MessageBox.Show(TEST.Example.myMethod().ToString());



What i mean now: Why dont work this:
PHP Library Class:
Code: Select all
namespace TEST {
  class Example {
    public static function myMethod () {
      return (string)'Hello World!';
    }
  }
}


C# Application:
Code: Select all
MessageBox.Show(TEST.Example.myMethod());



Im must set, on all Methods, in C# Application, ".ToString()". It looks not really great and i think, an better method exists, for this. ;-)

I can successfully call the PHP Class Library in C# Application, with method and im become result back, but only with .ToString(). Otherwise error is returned, object can not convert to string.
noname0x100
 
Posts: 13
Joined: March 17th, 2012, 12:37 pm

Re: type hint from php class library?

Postby Jakub Misek » May 8th, 2012, 4:19 pm

Right.

PHP is dynamic language, so its functions return always Object (or PhpReference if it is aliased using &). It is not possible to determine function's return type (compatible with C#) at compile time.

The only way would be to implement special attribute in Phalanger Core; where you would specify the type.

Anyway, '.ToString()' is not recommended way of casting returned objects. I would recommend to use PHP.Core.Convert.ObjectToString() (and other conversion routines here). This safely converts PHP object into compatible .NET object. (because string shouldn't be always String in PHP)
Jakub Misek │ DEVSENSE s.r.o. | @misekjakubjakub@devsense.com
User avatar
Jakub Misek
 
Posts: 2092
Joined: January 4th, 2012, 2:42 pm
Location: Prague


Return to Phalanger project

Who is online

Users browsing this forum: No registered users and 3 guests

cron

User Control Panel

Login

Who is online

In total there are 3 users online :: 0 registered, 0 hidden and 3 guests (based on users active over the past 5 minutes)
Most users ever online was 511 on April 27th, 2024, 10:11 pm

Users browsing this forum: No registered users and 3 guests