如上面的注释中所述,您应该分而治之,以使您的生活更轻松(尤其是当您在该大型函数中使用代码时编写代码)。确实很容易做到:
function file_put($number, $data){ $path = sprintf("C:/temp/wamp/www/file%d.txt", $number); file_put_contents($path, $data);}
例如,这只是替换许多重复的行,您只需要在其中放入一些(编号)文件即可。
但是您也可以使用更复杂的内容(例如数据库 *** 作)来执行此 *** 作。您可能希望将错误处理移到您的视线之外,并在需要时小心地连接到数据库,并以更灵活的方式来获取数据。可以通过将(已弃用的)
mysql_*函数移至其自己的一两个类中来完成,以使它不被看到。这将使它的使用更加容易(我首先显示):
// Create your database object to use it later on:$config = array( 'server' => 'localhost', 'name' => 'root', 'password' => '', 'db' => 'test',);$db = new MySql($config);
我称它为数据库类,
MySql因为它代表了mysql连接,并且可以与旧的mysql扩展一起使用。您只需要将该数据库对象传递给问题中的函数即可。结合
file_put功能,它看起来像这样:
function checkin(MySql $DB, $TechID, $ClientID, $SiteID){ $query = sprintf("SELECt `Type` FROM `Log` WHERe `TechID` = '%d' ORDER BY LogTime DESC LIMIT 1", $TechID); file_put(5, $query); $result1 = $DB->query("SELECt COUNT(*) FROM Log"); $result2 = $DB->query($query); foreach ($result1 as $row1) { list($count) = $row1; $data = "ClientID:$ClientID TechID:$TechID SiteID:$SiteID Count:$count" file_put(3, $data); foreach ($result2 as $row2) { file_put(4, $data); } }}
该
checkin函数仍然接近大型(已经有12行代码),但是比您的第一个版本短得多,因为它委派了编写文件和访问数据库的工作。我希望这个示范有用。以下是完整的代码示例:
class MySqlException extends RuntimeException{}class MySql{ private $server; private $name; private $password; private $db; private $connection; public function __construct(array $config) { $this->server = $config['server']; $this->name = $config['name']; $this->password = $config['password']; $this->db = $config['db']; } private function connect($server, $name, $password) { $this->connection = mysql_connect($server, $name, $password); if (!$this->connection) { $this->error("Unable to connect to '%s' as user '%s'", $server, $name); } } private function select($db) { if (!mysql_select_db($db, $this->connection)) { $this->error("Unable to select database '%s'", $db); } } private function close() { $this->connection && mysql_close($this->connection); } private function connectSelect() { $this->connect($this->server, $this->name, $this->password); $this->select($this->db); } public function query($query) { $this->connection || $this->connectSelect(); $result = mysql_query($query, $this->connection); if (!$result) { $this->error("Unable to execute query '%s'", $query); } return new MySqlResult($result); } private function error($format) { $args = func_get_args(); array_shift($args); $format .= ': %s'; $args[] = $this->connection ? mysql_error($this->connection) : mysql_error(); throw new MySqlException(vsprintf($format, $args)); } public function __destruct() { $this->close(); }}class MySqlResult implements Iterator, Countable{ private $result; private $index = 0; private $current; public function __construct($result) { $this->result = $result; } public function fetch($result_type = MYSQL_BOTH) { $this->current = mysql_fetch_array($this->result, $result_type); return $this->current; } public function current() { return $this->current; } public function next() { $this->current && $this->fetch(); } public function key() { return $this->current ? $this->index : null; } public function valid() { return (bool)$this->current; } public function rewind() { $this->fetch(); } public function count() { return mysql_num_rows($this->result); }}// Create your database object to use it later on:$config = array( 'server' => 'localhost', 'name' => 'root', 'password' => '', 'db' => 'test',);$db = new MySql($config);function file_put($number, $data){ $path = sprintf("C:/temp/wamp/www/file%d.txt", $number); file_put_contents($path, $data);}function checkin(MySql $DB, $TechID, $ClientID, $SiteID){ $query = sprintf("SELECT `Type` FROM `Log` WHERe `TechID` = '%d' ORDER BY LogTime DESC LIMIT 1", $TechID); file_put(5, $query); $result1 = $DB->query("SELECt COUNT(*) FROM Log"); $result2 = $DB->query($query); foreach ($result1 as $row1) { list($count) = $row1; $data = "ClientID:$ClientID TechID:$TechID SiteID:$SiteID Count:$count"; file_put(3, $data); foreach ($result2 as $row2) { file_put(4, $data); } }}checkin($db, 1, 2, 3, 4);
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)