OOP Wrapper for php features
PHP6 err PHP7 is gathering ideas on how to shape the language. This is my idea how the PHP API could look like. Please help and join in.
DISCLAIMER: DO NOT USE THIS (YET)! At the moment, this is my personal playground and everything is subject to change - ye'd be warned.
PHP had (especially in the past) no naming conventions for functions, which led to mixed function names (which is already adressed in the php core).
Examples:
// different naming conventions
strpos
str_replace
// totally unclear names
strcspn // STRing Complement SPaN
strpbrk // STRing Pointer BReaK
// inverted parameter order
strpos($haystack, $needle)
array_search($needle, $haystack)
As been done in Naming Conventions
PHP often provides a 'resource' type. However, most of the time we wrap them in classes anyway. Provide them out-of-the box.
Examples:
See ldap or ftp package.
Leverage the 'php' namespace. Wrap functions that serve under the same purpose as single units (= classes).
Using classes to collect functions basically isn't a good idea and never a good practice. However, I see one good reason to do so, which is a function collection for a specific context where a class which acts as namespace. E.g. if you are looking for a mathematical function but don't (fully) remember the name, type Math:: and your IDE can help you out with content assist, showing you only mathematical related functions.
Create units where possible.
Example: Filesystem
Create e.g. a Filesystem interface, which will also be used for FtpConnections or WebDav, etc. so the filesystem is interchangeable, but also extendable if you need a specific implementation (eg. Dropbox, AWS, Owncloud, etc.).
Add missing language, such as collections.
(provided collections are usable via its own package: gossi/collection)
Provide primitives as objects with methods to operate on them directly instead of functions that take the primitive as parameter. This would be more in common with current coding standards and would also improve readability.
Example:
ucfirst(strtolower($string)) // vs.
$string->lower()->upperFirst()
$str = 'hello world';
var_dump($str instanceof php\lang\String); // bool(true)
Object | Primitve |
---|---|
php\lang\String | String |
php\lang\Number | int, double, float |
php\lang\ArrayObject | Array |
Related Links:
With all the changes mentioned above, this would automatically add more type hinting, since objects are available, that can returned or taken as parameters.
As this project focusses on providing an API, run sami to create it:
$ vendor/bin/sami.php update sami.php
Very much playing around at this time, which revealed some insights:
A very rough plan for this:
...
-> Create Proposal at PHP to implement the API into the language.