type hint from php class library?
4 posts
• Page 1 of 1
type hint from php class library?
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?
Do you have an example?
Jakub Misek │ DEVSENSE s.r.o. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
Re: type hint from php class library?
PHP Library Class:
C# Application:
What i mean now: Why dont work this:
PHP Library Class:
C# Application:
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.
- 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?
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)
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. | @misekjakub │ jakub@devsense.com │
-
Jakub Misek - Posts: 2092
- Joined: January 4th, 2012, 2:42 pm
- Location: Prague
4 posts
• Page 1 of 1
Who is online
Users browsing this forum: No registered users and 7 guests