Eliminate Code Segregation: Writing Cleaner, More Elegant PHP
This article shows how to transform fragmented PHP snippets—such as disjointed loops, scattered timing code, and split error‑code definitions—into cohesive, readable structures using for‑loops, functional helpers, shutdown callbacks, and modern constant arrays, improving both aesthetics and maintainability.
The author, a former Go enthusiast, draws a parallel between the beauty of a well‑shaped board and the elegance of clean code, arguing that code should avoid "segregation"—the unnecessary separation of logically related parts.
Loop Refactoring
A typical while‑loop with a separate increment creates a fragmented feel:
<?php
$i = 0;
while ($i <= 10) {
echo $i;
$i++;
}
?>Replacing it with a for‑loop unifies the initialization, condition, and increment:
<?php
for ($i = 0; $i <= 10; $i++) {
echo $i;
}
?>Even more concisely, a functional style can be used:
<?php
array_map(function($i) { echo $i; }, range(0, 10));
?>Keeping Timing Code Together
Measuring execution time with separate start and end statements also creates a split:
<?php
$begin = microtime(true);
// ... code ...
$end = microtime(true);
echo $end - $begin;
?>Using register_shutdown_function binds the end‑time logic to the start‑time variable, ensuring they stay together:
<?php
$begin = microtime(true);
register_shutdown_function(function() use ($begin) {
$end = microtime(true);
echo $end - $begin;
});
// ... code ...
sleep(1);
?>The same idea can be applied in other languages via decorator patterns.
Unified Error‑Code Definitions
Separating constants from their messages leads to disjointed error handling:
<?php
class Result {
const ERROR_USERNAME = 1;
const ERROR_PASSWORD = 2;
public static $msg = array(
1 => '用户名错误',
2 => '密码错误',
);
}
?>Combining code and message in an associative array removes the split:
<?php
class Result {
public static $msg = array(
'ERROR_USERNAME' => '用户名错误',
'ERROR_PASSWORD' => '密码错误',
);
}
?>Modern PHP allows constants to hold arrays, preserving numeric identifiers while keeping code and message together:
<?php
class Result {
const ERROR_USERNAME = [1, '用户名错误'];
const ERROR_PASSWORD = [2, '密码错误'];
}
?>An exception class can then consume these structures:
<?php
class ResultException extends Exception {
public function __construct(array $result = array()) {
if ($result) {
parent::__construct($result[1], $result[0]);
}
}
}
throw new ResultException(Result::ERROR_USERNAME);
?>Final Thoughts
While a dedicated class for error codes can become a "mute class" and may be better distributed across business objects, the main lesson remains: keep related code elements together to improve readability, maintainability, and aesthetic quality.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
ITPUB
Official ITPUB account sharing technical insights, community news, and exciting events.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
