国产成人精品亚洲777人妖,欧美日韩精品一区视频,最新亚洲国产,国产乱码精品一区二区亚洲

您的位置:首頁技術(shù)文章
文章詳情頁

跨類調(diào)用后,找不到方法

瀏覽:135日期:2022-06-02 09:59:24

問題描述

做靜態(tài)方法重載實現(xiàn)數(shù)據(jù)庫的鏈式訪問的練習

demo5.php引用了Query.php的類

然后訪問Query中的方法,頁面提示我方法 'table' 在 Database 中未找到

以下是源代碼,希望可以幫我檢查出錯誤

第一個是Query.php

<?php//常用的數(shù)據(jù)查詢操作class Query{ //連接對象 public $pdo = null; //數(shù)據(jù)表名稱 public $table = ''; //字段列表 public $field = ''; //查詢條件 public $where = ''; //顯示數(shù)量 public $limit = 0; //構(gòu)造方法 public function __construct(PDO $pdo) {$this->pdo = $pdo; } //調(diào)用表名 public function table($tableName) {$this->table = $tableName;//關(guān)鍵是這一步return $this; } //調(diào)用字段 public function field($fields) {$this->field = $fields;//關(guān)鍵是這一步return $this; } //設(shè)置查詢條件 public function where($where) {$this->where = $where;return $this; } //設(shè)置顯示數(shù)量 public function limit($limit) {$this->limit = $limit;return $this; } //創(chuàng)建SQL語句查詢 public function select() {//設(shè)置查詢條件$fields = empty($this->field) ? '*' : $this->field;$where = empty($this->where) ? '' : ' WHERE ' . $this->where;$limit = empty($this->limit) ? '' : ' LIMIT ' . $this->limit;//SQL$sql = 'SELECT '.$fields. 'FROM' .$this->table. $where . $limit;//預處理執(zhí)行$stmt = $this->pdo->prepare($sql);$stmt->execute();return $stmt->fetchAll(PDO::FETCH_ASSOC); }}

第二個是demo5.php

<?php//方法重載實例演示require 'Query.php';class Database{ //數(shù)據(jù)庫連接對象 protected static $pdo = null; //數(shù)據(jù)庫連接方法,每次查詢時再連接,實現(xiàn)真正的惰性連接,節(jié)省系統(tǒng)開銷 public static function connection() {self::$pdo = new PDO('mysql:host=127.0.0.1;dbname=php','root','admin'); } //靜態(tài)方法的重載,實現(xiàn)跨類調(diào)用 public static function __callStatic($name,$arguments) {//連接上數(shù)據(jù)庫self::connection();//實例化查詢類$query = new Query(self::$pdo);//訪問Query中的方法return call_user_func_array([$query, $name],[$arguments[0]]); }}$cats = Database::table('category') ->field('cate_id, name, alias') ->where('cate_id>=2') ->select();foreach($cats as $cat){ print_r($cat);}

問題解答

回答1:

找到問題了,是Query.php的第65行,做拼接的時候,F(xiàn)ROM前后應(yīng)該加上空格。

回答2:

頁面提示方法 'table' 在 Database 中未找到

跨類調(diào)用后,找不到方法

相關(guān)文章:
主站蜘蛛池模板: 东光县| 平乐县| 塔河县| 临江市| 门头沟区| 岑巩县| 资阳市| 房山区| 建始县| 高密市| 自治县| 怀宁县| 晋江市| 寻甸| 三台县| 丰城市| 连南| 灵山县| 新河县| 南郑县| 灵丘县| 德令哈市| 得荣县| 芦溪县| 扎鲁特旗| 蒙城县| 安平县| 平湖市| 澜沧| 茶陵县| 宝鸡市| 乐亭县| 泾川县| 读书| 志丹县| 尉氏县| 页游| 龙井市| 隆昌县| 永寿县| 鹿泉市|