开发者

Convert PHP procedural code into OOP method chaining...are maintainability and readability worth the trouble?

开发者 https://www.devze.com 2023-03-28 17:37 出处:网络
I had a medium-size task done in procedural style so I thought of converting the code to OOP for ease of maintenance. The original code was a big block in a single file and what I did was:

I had a medium-size task done in procedural style so I thought of converting the code to OOP for ease of maintenance. The original code was a big block in a single file and what I did was:

- Break down the task into functions which handle a part of the task

- Put the functions into a class for method chaining

After conversion, the code become like this:

$obj开发者_高级运维 = new Class();
$result = $obj->task1()->task2()->task3()->task4()->getResult();

I can either leave it like this or further convert each task into a class for more abstraction. What are your takes on refactoring code just for maintainability and readability (and possibly portability once I break down the functions further and convert them into classes) ?


Classes are not bags of functions, and method chainning is not a valid reason for moving to OOP. Read about OOP principles and what it can do better than procedural.

0

精彩评论

暂无评论...
验证码 换一张
取 消