基类文件class.baseweb.php
<?php // 主要用于继承子类继承,直接调用父类函数实现功能 class baseweb { const warnDbname = "sqlite:/dat/warnmsg.dat"; const realData = "sqlite:/root/rundata.dat"; const paramConf = "sqlite:/dat/paramconf.dat"; // 数据库返回数据 protected $dbReply = ""; // 整体json格式 public $totalJson = ""; protected $user = ""; protected $password=""; /** * Debug level for no output */ const DEBUG_OFF = 0; /** * Debug level to show client -> server messages */ const DEBUG_CLIENT = 1; /** * Debug level to show client -> server and server -> client messages */ const DEBUG_SERVER = 2; /** * Debug level to show connection status, client -> server and server -> client messages */ const DEBUG_CONNECTION = 3; /** * Debug level to show all messages */ const DEBUG_LOWLEVEL = 4; // debug判断 protected $error = array( 'error' => '', 'detail' => '', 'msg_code' => '', 'msg_code_ex' => '' ); /** * Debug output level. * Options: * * self::DEBUG_OFF (`0`) No debug output, default * * self::DEBUG_CLIENT (`1`) Client commands * * self::DEBUG_SERVER (`2`) Client commands and server responses * * self::DEBUG_CONNECTION (`3`) As DEBUG_SERVER plus connection status * * self::DEBUG_LOWLEVEL (`4`) Low-level data output, all messages * @type integer */ public $do_debug = self::DEBUG_OFF; /** * How to handle debug output. * Options: * * `echo` Output plain-text as-is, appropriate for CLI * * `html` Output escaped, line breaks converted to `<br>`, appropriate for browser output * * `error_log` Output to error log as configured in php.ini * * Alternatively, you can provide a callable expecting two params: a message string and the debug level: * <code> * $smtp->Debugoutput = function($str, $level) {echo "debug level $level; message: $str";}; * </code> * @type string|callable */ public $Debugoutput = 'echo'; /** * Get the latest error. * @access public * @return array */ public function getError() { return $this->error; } /** * Set error messages and codes. * @param string $message The error message * @param string $detail Further detail on the error * @param string $smtp_code An associated SMTP error code * @param string $smtp_code_ex Extended SMTP code */ protected function setError($message, $smtp_code = '',$detail = '', $smtp_code_ex = '') { $this->error = array( 'error' => $message, 'msg_code' => $smtp_code, 'detail' => $detail, 'msg_code_ex' => $smtp_code_ex ); } protected function edebug($str='', $level = 0) { if(empty($str)) $str = $this->error['error']; if ($level > $this->do_debug) { return; } //Avoid clash with built-in function names if (!in_array($this->Debugoutput, array('error_log', 'html', 'echo','return')) and is_callable($this->Debugoutput)) { call_user_func($this->Debugoutput, $str, $this->do_debug); return; } switch ($this->Debugoutput) { case 'error_log': //Don't output, just log error_log($str); break; case 'html': //Cleans up output a bit for a better looking, HTML-safe output echo htmlentities( preg_replace('/[\r\n]+/', '', $str), ENT_QUOTES, 'UTF-8' ) . "<br>\n"; break; case 'echo': default: //Normalize line breaks $str = preg_replace('/(\r\n|\r|\n)/ms', "\n", $str); // echo gmdate('Y-m-d H:i:s') . "\t" . str_replace( // "\n", // "\n \t ", // trim($str) // )."\n"; echo '{"return_code":"'.$this->error['msg_code'].'","return_msg":"'.addslashes($str).'"}'; } } // 用户认证 // check user privilege protected function userauth() { // $passwordStr = md5($this->password); $sql = "select * from t_web_user where user_name='".$this->user."' and user_Password='".$passwordStr."'"; try { $dbh = new PDO(self::paramConf, null, null); $dbh->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION); $sth = $dbh->query($sql); $sth->setFetchMode(PDO::FETCH_ASSOC); $result = $sth->fetch(); $dbh = null ; } catch (PDOException $e) { $this->setError($e->errorInfo[2],$e->errorInfo[1],$e->getMessage(),$e->getCode()); $this->edebug(); $dbh = null; exit(); } // print_r($result); if(!is_array($result)) { $this->setError(Error::$errArr[26],26); $this->edebug(); // return false; exit(); } } } ?>
继承类class.relaytime.php
<?php require_once "class.baseweb.php"; /** * 主要负责将内容写入到数据库 */ // static 不会调用构造函数 relaytime::GetData(); class relaytime extends baseweb { function __construct() { print_r($_POST); } public static function GetData() { print_r($_POST); } } ?>
调用class.relaytime.php的文件relay_time.php
<script src="jquery-1.11.1.min.js" type="text/javascript" charset="utf-8"></script> <script type="text/javascript"> function upload() { // console.log($("#relayTimeForm").serialize()); $.ajax({ type: 'post', url: 'class.relaytime.php', data: $("#relayTimeForm").serialize(), success: function(data) { alert(data); } }); } </script> <div align="center"> <form action="" method="post" id="relayTimeForm" accept-charset="utf-8"> <table> <thead> <tr> <th colspan="4">定时执行页面设置</th> </tr> </thead> <tbody> <tr> <td>开始时间</td> <td>间隔时间</td> <td>执行次数</td> <td>执行路径</td> </tr> <?php // die(""); for ($i=0; $i < 10; $i++) { echo "<tr>"; $keyArr = array("start_time","interval_time","exec_num","exec_path"); for ($j=0; $j < count($keyArr); $j++) { echo "<td>"; echo "<input type=\"text\" name='$keyArr[$j]$i' value='' placeholder=''>"; echo "</td>"; } echo "</tr>"; } ?> </tbody> </table> <input type="reset" name="" value="重新填写"> <input type="button" name="" value="提交" onclick="upload()"> </form> </div>
当post数据给类class.relaytime.php时会出现class relaytime not found的错误。
将 class.relaytime.php文件里relaytime::GetData();放在底部则不会出现错误。
在解释型语言当中,对文件的解释执行都是执行进行的,所以在调用函数时,确保被调用函数在前面先被定义好。
个人理解:
在php文件里,如果没有require/include文件时,php解释器会将这个文件的各个依赖关系处理好,而不必管理位置的存放。
而有include/require其它文件时,则要处理函数调用间的位置存放,避免出现上述class not found的情况。