Do you know of any project that redefines native PHP types as objects? Is it possible?

Do you know of any project that overrides native PHP types (string, array, ints, floats, bools, etc.) as objects? I'm not sure how possible this is, but I just thought it would be nice to have native types as objects.

Example:

$Name = new String('Mary had a little lamb.'); print($Name->Length); //prints 23 print($Name->Replace('/lamb/', 'duck')); // prints Mary had a little duck. 

Will this add a lot of overhead? Think about it?

+4
source share
2 answers

There are many, many (even native) projects that are aimed at this: the fact is that no one uses them.

There are basically two problems:

  • PHP has only limited operator overloading. You cannot overload the + operator. So, to sum two numbers, you need to write $number->add($number2) , which is not very intuitive.
  • PHP has many predefined functions. It can be argued that PHP has the most powerful standard library of all programming languages. But: all of these functions return native types, not boxed ones. So you need to write something like $number = new Number(function_returning_number()); . This also applies to third-party libraries.

There is a PECL extension for the first release. It is not PHP related, although it is not included in regular PHP installations. Therefore, you cannot use it for portable applications.

To solve the second problem, there is Autoboxing RFC . Perhaps this will be realized, perhaps not.

+6
source

Perhaps someone can clarify this, since I'm not fully informed on this issue, but PHP works on the Zend engine on the right (C ++), and therefore, if you are experienced enough, you can probably implement your idea.

0
source

All Articles