Closed thekid closed 7 years ago
A working version is available at https://github.com/thekid/xp-framework/compare/rfc-236 - the thing is though that these classes are completely useless for the framework itself. Maybe another place like providing these inside the compiler could be more benefitial.
This here https://github.com/thekid/xp-language/compare/rfc-236 would move the (from a framework point of view - useless) class to the compiler. We would need to bundle a compiled version for runtime support in the cases methods cannot be completely resolved, though (e.g. "hello".length();
will be resolved to StringExtensions::length('hello');
in the first step, but with inlining applied, could easily become strlen("hello");
and thus have no more dependencies on this class at runtime).
Scope of Change
Extension methods supported by classes inside
lang.types
will replace native function calls in XP language.Rationale
When writing XP language code, every PHP function needs to be imported via
import native
. The reason for this is that native function usage makes code more difficult to port to different platforms (e.g., HipHop, or a future NodeJS platform, for example).Functionality
To discover native function usage in an XP language project, one can use the grep utility:
Exemplary analysis
For the Dialog photoblog software, the top 10 functions used are:
sprintf
,unserialize
,sscanf
,serialize
,substr
,sizeof
,strpos
,is_file
,file_get_contents
anddirname
.Side note: Especially PHP's serialization mechanism has had multiple problems in the past - accessibility levels (public / private / protected in PHP 4 -> PHP 5) and namespaces vs. non-namespaces (PHP 5.2 -> PHP 5.3) and should thus be replaced by a completely separate functionality in the framework - see RFC #6!
Looking at the next 10 most often used functions, we see:
basename
,array_keys
,usort
,strolower
,str_replace
,krsort
,implode
,preg_replace
,create_function
andmax
.Looking at xp-framework/xp-contrib#9
The top 10 PHP functions used are:
strpos
,in_array
,substr
,count
,array_pop
,explode
,str_replace
,strtolower
,sprintf
andstrlen
.The next 10 most commonly used functions:
ucfirst
,strtoupper
,str_repeat
,print_r
,implode
,chr
,basename
,preg_replace
,token_name
andtoken_get_all
.Checking for XP Core converted via xp-framework/xp-language#7
The top 10 PHP functions used are:
sprintf
,substr
,strle
,sizeof
,array_keys
,implode
,strpos
,is_array
,explode
andsscanf
.The next 10 most commonly used functions:
strtolower
,trim
,func_get_args
,str_replace
,strtr
,rtrim
,is_resource
,call_user_func_array
,array_shift
andin_array
.Observation
The most commonly used PHP functions are string and array functions.
Arrays
Strings
Security considerations
Speed impact
Slower for the unoptimized case, because code such as
$string.substr(0, 5);
actually gets transformed toStringExtensions::substr($string, 0, 5);
which internally calls the native function. If we can inline these calls, then we save ourselves the overhead for the static method call.Dependencies
Related documents