无法从PHP中的其他非静态类调用类中的静态函数

wkftcu5l  于 2023-05-05  发布在  PHP
关注(0)|答案(2)|浏览(134)

我现在尝试了12个多小时,我真的很沮丧,因为它不起作用。
我有两个类BookComment。每本书都有多条评论。我想返回一本特定的书及其n条评论。为此,我创建了两个类,因为每个类都应该有额外的操作,如添加,删除,更新等。
我的问题是,当我从book类的非静态函数调用Comment类中的静态函数时,它不起作用:

$result = Comment::GetCommentByBookId($id);

奇怪的是,当我将Comment类中的静态函数GetCommentByBookId更改为非静态函数,并将GetBookById函数中的行更改为

$c = new comment;
$result = $c->getCommentByBookId(1);

它工作!但是为什么当我尝试调用静态函数时它不工作?
这些是类:

预订

<?php
    require_once ("class/DBController.php");
    require_once ("class/Comment.php");

    class Book {
        private $db_handle;

        function __construct() {
            $this->db_handle = new DBController();
        }
        
        function GetBookById($id) {
            $query = "SELECT * FROM book WHERE id = ?";
            $paramType = "i";
            $paramValue = array(
                $id
            );
            
            $c = new comment;
            $result = $c->getCommentByBookId(1);
            //$result = Comment::GetCommentByBookId($id);
            return $result;
        }

        function GetAllBooks() {
            $query = "SELECT * FROM book";
            $result = $this->db_handle->runBaseQuery($query);
            return $result;
        }
    }
?>

留言

<?php
    require_once ("class/DBController.php");

    class Comment {
        private $db_handle;

        function __construct() {
            $this->db_handle = new DBController();
        }
        
        static function GetCommentByBookId($id) {
            echo "Entered 'GetCommentByBookId'<br>";
            $query = "SELECT * FROM comment WHERE book_id = ?";
            $paramType = "i";
            $paramValue = array(
                $id
            );
            
            echo "Pre-Ex 'GetCommentByBookId'<br>";
            
            $result = $this->db_handle->runQuery($query, $paramType, $paramValue);
            
            echo "Executed successfully 'GetCommentByBookId'<br>";

            return $result;
        }
    }
?>
ars1skjm

ars1skjm1#

如果你没有示例化一个Comment示例,你就不能访问$this-〉db_handle。
你可以考虑像下面这样做:

static function GetCommentByBookId($id) {
     echo "Entered 'GetCommentByBookId'<br>";
     $query = "SELECT * FROM comment WHERE book_id = ?";
     $paramType = "i";
     $paramValue = array(
        $id
     );
            
     echo "Pre-Ex 'GetCommentByBookId'<br>";
            
     $result = (new DBController())->runQuery($query, $paramType, $paramValue);
            
    echo "Executed successfully 'GetCommentByBookId'<br>";

    return $result;
}
a6b3iqyw

a6b3iqyw2#

**问题:**但是为什么调用静态函数不起作用?
**答案:**因为当你调用类外的静态方法时,该类的构造不会运行,因此如果你调用依赖于构造类的任何其他属性,你会得到错误。

在你的例子中,当你在静态方法中调用static function GetCommentByBookId($id)时,你现在调用的是$result = $this->db_handle->runQuery($query, $paramType, $paramValue);,因为构造没有运行,因此当你调用静态方法时,this->handle没有示例化这个类,因此你得到了$this->db_handle->runQuery的错误。

示例

我已经重建了你的代码,你解释我的意思。请看一下代码和结果图像。

<?php 
error_reporting(E_ERROR |  E_PARSE);
ini_set('display_errors', '1');

 class Comment {

         function __construct() {
            $this->dbconnection = new DBController();
            echo "<br>construct was invoked and this msg is from construct of the comment class <br>";
            
        }
        
        //static way
         static function GetCommentByBookId($id) {
         $result = new DBController();
         $result = $result->runQuery('query here');

           return $result."here are the comments for Book ID - " . $id;
        }

        //normal way
        function GetCommentByBookId2($id) {
        $result = $this->dbconnection;
         $result = $result->runQuery('query here');

           return $result."here are the comments for Book ID - " . $id;
        }

    }



class DBController{

    function runQuery(){
        return "database connected and query was executed <br>";
    }
}



class Book {
             
        //this function uses normal method in comment class
        function GetBookById($id) {
            $c = new comment;
            $result = $c->GetCommentByBookId2(1);
            // $result = Comment::GetCommentByBookId($id);
            return $result;
        }

         //this function uses static method in comment class. No class instanciated. 
        function GetBookById2($id) {
            
            $result = Comment::GetCommentByBookId($id);
            return $result;
        }

       
    }

//using classes.

echo '<hr> <strong>Without Static Method</strong> <br> Here we called normal public method defined in the comments class. Here constructor is invoked<br><br><br>';

$bookinfo = new Book;
echo $bookinfo->GetBookById(2) ;

echo '<hr> <strong>Static Method</strong> <br> Here we called the static method defined in the comments class. We get the result because we are instanciating the DB class in static method. In static Construct did not run<br><br>';
echo $bookinfo->GetBookById2(3) ;
 ?>
  • 结果:* x1c 0d1x

如果您有任何疑问/问题或发现不正确的东西,请评论。

相关问题