Faster function calling
We have call_user_func($f,$a1,$aN)
, but it's been superseded with $f($a1,$aN)
.
However, there's no such thing for call_user_func_array($f,$args)
.
My proposal is to create a specific language syntax for this, such as $f{$args}
.
The reason everyone should stay a mile away from call_user_func*
is that they're extremely slow and ugly looking in the sense that there are better alternatives.
Object decleration syntax
Right now, to create an object on the fly, you need: (object)array('prop'=>'value');
. By convention, we should also have object('prop'=>'value');
. Also, short syntaxes would be handy, similar to JSON.
A be-all-end-all magic method for types
Right now, we have __toString()
, and many suggested __toInt
/__toFloat
/etc. My advice would be to implement __toType()
or __typecast()
, which as a first parameter, the desired data type is passed, eg:
class Test {
public function __toType($type){
switch($type){
case 'integer':
return (int)$this->age;
case 'string':
return $this->age.' years';
default:
throw new EUnsupportedTypeException();
}
}
}
If we wanted to be more specific, we could add another argument after $type, namely $class. So you can: if($class=='person')return new Person($this->age);
Specifying Data Type in foreach
Currently, you can specify the data type of a PHP function argument, like so:
public function say_hello_to(UserClass $user){
$this->say('Hello, '.$user->name.'!');
}
It would be great to do this in a foreach as well:
public function on_enter_office(){
foreach($users as UserClass $user) // <- See UserClass here?
$user>say_hello_to($this);
}
The current "fix" is using a closure, like so:
public function on_enter_office(){
$users->each(function(UserClass $user){
$user>say_hello_to($this);
});
}
The fix takes more resources, more writing and messes the scope, hence why a native solution will make it easier, cleaner and probably faster than the current fix.
Conditional Defines
This probably won't be a useful feature for many people, but it is a great way to keep the running code at a minimum even when it is compatible with old systems, making execution faster. Consider the following code:
if(!function_exists('json_encode')){
function json_encode($value, $options=0){
// legacy code
}
}
- The
// legacy code
section is still parsed, hence any errors in there will cause PHP to quit.
- Parsing it also makes PHP slower, even if it doesn't need it at all.
- The code is not intuitive to developers
- Any IDE parsing engines will get confused since they ignore if statements and end up listing the function twice.
The fix? Conditional compilation:
#if PHP_VERSION<5.2
function json_encode($value, $options=0){
// legacy code
}
#endif