当试图在另一个类中打开扩展类时发生错误..
mydb.php
<?php
class mydb{
public $conn;
function __construct(){
$this->conn=new mysqli("localhost","root","","akshaya");
$this->_conn=$this->conn;
if(!$this->conn){
echo "mysql connection error";
}else{
return $this->conn;
}
}
}
?>
login.class.php
<?php
include_once __DIR__.'\..\mydb.php';
class vishnuADMIN extends mydb {
public function login($uname,$password){
$uname=$this->conn->real_escape_string($uname);
$password=$this->conn->real_escape_string($password);
$password="s4l8".sha1($password);
$q="select * from user where uname='$uname' and password='$password'";
$res=$this->conn->query($q);
if(mysqli_num_rows($res)){
// (!isset($_SESSION))? session_start() :"";
$_SESSION["user"]=$uname;
return true;
}else{
return false;
}
}
public function register($uname,$password){
$password="s4l8".sha1($password);
echo $q="INSERT INTO user(uname,password) VALUES ('$uname','$password')";
$res=$this->conn->query($q);
if($res){
(!isset($_SESSION))?session_start():"";
$_SESSION["user"]=$uname;
return true;
}else{
return false;
}
}
public function logincheck(){
// (!isset($_SESSION))?session_start():"";
if(isset($_SESSION["user"])){
return true;
}else{
return false;
}
}
public function logout(){
// (!isset($_SESSION))?session_start():"";
session_unset();
}
}
//$obj=new vishnuADMIN();
//if($obj->logincheck()){
// echo "hai";
//}
cms.class.php
<?php
class paginator_vishnukumar extends mydb{
private $_limit,$_page,$_query,$_total,$_conn;
public function __construct( $query ) {
$this->_query = $query;
$rs= $this->conn->query( $this->_query );
$this->_total = $rs->num_rows; }
public function getData( $page = 1,$limit = 10 ) {
$this->_limit = $limit;
$this->_page = $page;
/*
some blah blah codes...
*/
}
public function createLinks( $links, $list_class ) {
if ( $this->_limit == 'all' ) {
return '';
/*
some blah blah codes...
*/
}
return $html;
}
}
?>
admin.php
<?php
require 'engine/vishnuHTML.class.php';
require 'engine/admin/login.class.php';
$html=new vishnuHTML();
(!isset($_SESSION))?session_start():"";
$html->head();
$html->navigation();
$log=new vishnuADMIN();
/* vishnuADMIN::login("vishnu","vishnukumar"); */
if(isset($_POST['submit'])){
if($log->login($_POST['uname'],$_POST['password'])){
logedin();
}else{
echo "invalid";
}
}
if($log->logincheck()){
logedin();
}else{
loginform();
}
?>
<?php
$html->footer();
function loginform(){
?>
<div class="row">
<div class="login columns">
<div class="four column">
<form method="post" action="">
<label>User name</label>
<input type="text" name="uname"><br>
<label>Password</label>
<input type="password" name="password"><br>
<input type="submit" value="go" name="submit"><br>
</form>
</div>
</div>
</div>
<?php
}
function logedin(){
require 'engine/admin/cms.class.php';
$limit = ( isset( $_GET['limit'] ) ) ? $_GET['limit'] : 25;
$page = ( isset( $_GET['page'] ) ) ? $_GET['page'] : 1;
$links = ( isset( $_GET['links'] ) ) ? $_GET['links'] : 7;
$query = "SELECT * FROM pages";
$Paginator = new paginator_vishnukumar( $query );
$results = $Paginator->getData( $page, $limit );
}
?>
请检查此代码
抛出错误: fatal error :在第 7 行的 C:..\engine\admin\cms.class.php 中对 null 上的成员函数 query() 调用
login.class.php 和 cms.class.php 中的扩展类 mydb 如何在多个页面/类中使用连接变量类..
最佳答案
您需要在 cms.class.php 中调用 parent::__construct();
<?php
class paginator_vishnukumar extends mydb{
private $_limit,$_page,$_query,$_total,$_conn;
public function __construct( $query ) {
parent::__construct(); // <-- will initiate the connection
$this->_query = $query;
$rs= $this->conn->query( $this->_query );
// ....
}
}
关于php OOP 错误 : Call to a member function query() on null in c:blah/bla/. ./\engine\admin\cms.class.php 第 7 行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36418497/