PHP: Real world OOP example

后端 未结 7 647
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-04 05:53

I am trying to learn OOP. The so called \'real world\' examples in the books I am reading aren\'t helping.

All the examples like Pet, Car

7条回答
  •  北海茫月
    2020-12-04 06:43

    I suggest also to see my wrapper Arrayzy. It's a native PHP arrays easy manipulation library in OOP way.

    So if you work with native PHP array functions - you could do the same things in OOP and Arrayzy helps you with it, for example:

    // Functional programming:
    $array = ['a', 'b', 'c'];
    $resultArray = array_merge($array, ['c', 'd']);
    

    and

    // Object-oriented programming:
    $obj = Arrayzy\MutableArray::create(['a', 'b', 'c']);
    $obj->mergeWith(['c', 'd']);
    $resultArray = $obj->toArray();
    

    In both cases the result array will be:

    Array(
        0 => 'a'
        1 => 'b'
        2 => 'c'
        3 => 'c'
        4 => 'd'
    )
    

    Check how does this mergeWith method (or other) works under the hood.

    I think this is a nice example which shows that almost everything functional code you could replace with OOP code like in this library. But with OOP you get much more and you could also check Functional programming vs OOP question to learn more details what's a cons and props of it.

提交回复
热议问题