Is there a way to tell the php complier that I want a specific implicit conversion from one type to another?
A simple example:
class Integer
{
public $
Actually this possible with the SPL_Type extension. It defines classes for primitive types whose objects operate interchangeably with the PHP's primitive types (booleans, enums, ints, floats and strings).
You first example would work by simply inheriting from SplIt as follows:
class Integer extends extends \SplInt{}
function ExampleFunc(Interger $i){...}
ExamFunc(333); // 333 -> Integer object with $val == 333.
The problem is that the code of the SplTypes extension hasn't been maintained since 2012, and by the moment of this post, it hasn't ported to PHP 7, nor there is a plan for such.
UPDATE I have found the following unnofficial port of SPL Types to PHP 7. Use at your own risk: https://github.com/esminis/php_pecl_spl_types
Basic installation instructions:
phpize
../configure
make && make install
echo "extension=spl_types.so" > /etc/php7/conf.d/spl_types.ini
service php7.0-fpm restart
Are you asking how to type cast? You can do:
$ php -r 'var_dump("333");'
string(3) "333"
$ php -r 'var_dump((int)"333");'
int(333)
Otherwise PHP is weakly typed, so in general you don't need to do it. It's implied by the language. Eg. if a function takes a numeric argument, then that number can be string, integer or float all the same; The value is converted if needed. That's why you can for example echo 33
without an error.
This question is very old, but I would like to give a solution. This behaviour is called boxing which can be done in PHP, but the inverse of this, unboxing see:
function a(int $a){...}
a(new Integer(1));
is not possible currently. If you want a framework which offers you this and even more, look at the BlazeFramework which has such wrapper classes.
Operation overloading does not work either in php. The implementation is maybe a bit too much to write, but I give you a hint! PHP calls the callback set with set_error_handler()
with the error level E_RECOVERABLE_ERROR
and debug_backtrace()
delivers information about the function/method call.
If you don't get that, look at the implementation of the method blaze\lang\System#systemErrorHandler()
, but possibly it is better to just use the framework if you want more of such features.
PHP5 has type hinting, with limitations: http://ca2.php.net/manual/en/language.oop5.typehinting.php
Specified types must be objects or array, so built in types such as string and int are not allowed.
This is not a conversion, but will throw an error if an object of the specified type is not passed to the method or function, as in your example.
No, PHP is not a strongly typed language.
Long answer:
I think it is very difficult (read impossible) for PHP to do an implicit conversion in this case.
Remember: the fact that you call your class Integer is a hint to the human reader of the code, PHP does not understand that it actually is used to hold an integer. Also, the fact that it has an attribute called $val is a hint to a human reader that it should probably contain the value of your integer. Again PHP does not understand your code, it only executes it.
At some point in your code you should do an explicit conversion. It might be possible that PHP has some nice syntactig sugar for that, but a first attempt would be something like:
class Integer
{
public $val;
function __construct($val) { $this->val = $val; }
}
function ExampleFunc($i){
if (is_numeric($i)) { $iObj = new Integer($i); }
...
}
ExamFunc(333); // 333 -> Integer object with $val === 333.
This is not as cool as you would like it, but again, it is possible that PHP has some syntactic sugar that will hide the explicit conversion more or less.
Short version:
In one way or another, you will need an explicit conversion