Object-Oriented PHP

Coding Critique is the place to post source code for peer review by other members of DevNetwork. Any kind of code can be posted. Code posted does not have to be limited to PHP. All members are invited to contribute constructive criticism with the goal of improving the code. Posted code should include some background information about it and what areas you specifically would like help with.

Popular code excerpts may be moved to "Code Snippets" by the moderators.

Moderator: General Moderators

Post Reply
nbaksalyar
Forum Newbie
Posts: 1
Joined: Thu Nov 12, 2009 1:14 am

Object-Oriented PHP

Post by nbaksalyar »

Hello!

We want to present you new library's preview, which is used to work with PHP's primitives (such as: string, integer, float and array) in object-oriented way.

Though we've always loved PHP, we had never understood its rough language design.
On the one side developers of PHP forgets about backward compatibility, on other — they don't even try to introduce any coding standards.

Objectives of this library is not in demonstrating of neither authors' nor PHP's skills and possibilities, but in full-fledged usage in development of production projects. Of course, with all benefits given by such approach - "real" type hinting, classes inheritance, etc.

Also, one of the objectives was the replacement for strange and alogical function names (for example, array_flip, but sort — or implode and str_replace. There is a hundreds of examples, actually).

And so, here is the example of library usage:

Code: Select all

 
<?
$map
    ->clear(function ($key, $val) {
        // Delete all elements, where key equals value
        return !$key->equals($val);
    })
    ->join(' ') // String
    ->replace('.', 'dot')
    ->pregReplace('/([0-9]+)/', function ($m) {
            return "!$m[]!"; // Some string with number(e.g. !12345!), is here
    })
    ->changeCase(Str::TITLE, Str::UP_FORCED)
    ->insert('[inserted]', 5)
    ->length() // Number
    ->multiply(4)
    ->add(6, 9, new Number(15))
    ->divided(5)
    ->sum(Number::EVEN)
    ->dump() // (Number) 1062.51
    ->root(4)
    ->round(3) // Just 3 symbols after dot
    ->dump() // (Number) 5.709
    ->round(Number::UP) // round to up (ceil)
    ->hex('15abbf')
    ->toString() // String ('1420223')
    ->hash() // 'md5' as default
    ->dump(); // (String) '0d1b1558224c8f3b125cd905c378c9f7'
 
You can find more detailed description, examples, and, of course, the library itself here.

We're waiting for your criticism with impatience :)

Sincerely yours,
OOPHP maintainers.
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Re: Object-Oriented PHP

Post by josh »

Pretty robust code, some of the unit tests could be re-factored to more clearly show the relationships between inputs and outputs.

I would love to see it ported to a PHP extension so it could be used in a real project. You make excellent use of the new closure syntax

So you are all from Russia?
Post Reply