【问题标题】:Getting a function from another class in php从 php 中的另一个类获取函数
【发布时间】:2014-05-03 02:37:04
【问题描述】:

所以我试图从我在另一个名为 selectColor() 的类中创建的方法中获取页面上的结果;

我似乎无法弄清楚我这样做是否正确。在 PDO 中是​​否有特殊的方法可以做到这一点?

connect.php - 连接到数据库

<?php 
    //Initialize variables to make db connection
    $host = "localhost";
    $dbName = "stormfront_productions_test";
    $username = "root";
    $password = "root";

    try{
        $pdo = new PDO("mysql:dbname=$dbName;host=$host", $username, $password);
        echo 'Connection Successful';
    }catch(PDOException $e){
        $error = $e->getMessage();
        echo $error;
    }




?>

colors.class.php--所有函数

  <?php



class colors {

//SELECT
    function selectColor()
    {
        $sql= "SELECT * FROM colors";
        require 'model/connect.php';
        $stmt = $pdo->query($sql); 
        $row =$stmt->fetchObject();
        echo "<table><tr><td>" . $row->id . "</td>";
        echo "<td>" . $row->color . "</td></tr></table>";
    }

//INSERT
    function insertColor($color){
        $sql = "INSERT INTO colors(color) VALUES (
            :color"; 
        require 'model/connect.php';
        $stmt = $pdo->prepare($sql);                                 
        $stmt->bindParam(':color', $color);       
        $stmt->execute();
    }


//UPDATE
    function updateColor($color){
        $sql = "UPDATE colors SET color = :color; 
            WHERE id = :id";
        require 'model/connect.php';
        $stmt = $pdo->prepare($sql);                                  
        $stmt->bindParam(':color', $color);       
        $stmt->execute();        
    }


//DELETE
    function deleteColor($color){
        $sql = "DELETE FROM colors WHERE color =  :color";
        require 'model/connect.php';
        $stmt = $pdo->prepare($sql);
        $stmt->bindParam(':color', $color);   
        $stmt->execute();
    }
}// END OF colors class

colors.php--显示函数selectColor()的结果;

   <!DOCTYPE html>
<?php include 'model/functions/colors.class.php';
      include '/model/connect.php'?>

<!--
To change this license header, choose License Headers in Project Properties.
To change this template file, choose Tools | Templates
and open the template in the editor.
-->
<html>
    <head>
        <meta charset="UTF-8">
        <title></title>
    </head>
    <body>
         <?php
         $getConn = new $pdo();
    $showSelect = new colors();
    $showSelect->selectColor();
    ?>
    </body>
</html>

【问题讨论】:

  • 阅读一些关于面向对象编程的知识。它不仅仅是将一些松散相关的功能打包在一起。

标签: php mysql sql pdo


【解决方案1】:

首先你应该创建一个 colors 类的对象,然后才能访问它的方法 selectColor()

 <body>
    <?php
    $showSelect = new colors();
    $showSelect->selectColor();
    ?>
</body>

PHP:

use PDO;

class colors {

private $pdo;

public function __construct(){
   $host = "localhost";
   $dbName = "stormfront_productions_test";
   $username = "root";
   $password = "root";

   try{
      $this->pdo = new PDO("mysql:dbname=$dbName;host=$host", $username, $password);
      echo 'Connection Successful';
   }catch(PDOException $e){
      $error = $e->getMessage();
      echo $error;
   }
}

//SELECT
function selectColor()
{
    $sql= "SELECT * FROM colors";
    $stmt = $this->pdo->query($sql); 
    $row =$stmt->fetchObject();
    echo "<table><tr><td>" . $row->id . "</td>";
    echo "<td>" . $row->color . "</td></tr></table>";
}

//INSERT
function insertColor($color){
    $sql = "INSERT INTO colors(color) VALUES (
        :color"; 
    $stmt = $this->pdo->prepare($sql);                                 
    $stmt->bindParam(':color', $color);       
    $stmt->execute();
}


//UPDATE
function updateColor($color){
    $sql = "UPDATE colors SET color = :color; 
        WHERE id = :id";
    $stmt = $this->pdo->prepare($sql);                                  
    $stmt->bindParam(':color', $color);       
    $stmt->execute();        
}


//DELETE
function deleteColor($color){
    $sql = "DELETE FROM colors WHERE color =  :color";
    $stmt = $this->pdo->prepare($sql);
    $stmt->bindParam(':color', $color);   
    $stmt->execute();
}
} // END OF colors class

【讨论】:

  • 好的,我现在从colors.class.php 收到两条通知。我该如何纠正? 'code'( ! ) 注意:未定义变量:第 11 行 C:\xampp\htdocs\stormfront_productions_test\model\functions\colors.class.php 中的 pdo Call STack 2 0.0408 152376 colors->selectColor( ) ..\colors。 php:16 (!) 致命错误:调用 C:\xampp\htdocs\stormfront_productions_test\model\functions\colors.class.php 中非对象的成员函数 query() 第 11 行调用堆栈 2 0.0408 152376 颜色->selectColor( ) ..\colors.php:16
  • @user3562189 : 显示你的require 'model/connect.php'; 文件。这意味着你应该从数据库连接类扩展你的colors 类或在你的colors 类中启动一个数据库连接对象。你不能只需要一个文件。
  • 我是用包含还是这样做?你能告诉我一个例子或编辑我的代码吗?
  • @user3562189 :正如我所说,在您发布您的 model/connect.php 文件之前,我什么都做不了。您可以在 include 上使用 require - 这并不重要 - 但在另一个不扩展此对象类的类中使用它们之前,您不应该忘记初始化对象。
  • 在我的代码中看到上面我有它需要 model/connect.php 并且它仍然抛出相同的错误。我尝试了 ../model/connect.php、./model/connect.php、/model/connect.php 和 model/connect.php,但似乎都没有摆脱错误,知道吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-08-10
  • 2018-06-30
  • 1970-01-01
  • 1970-01-01
  • 2014-01-22
  • 1970-01-01
相关资源
最近更新 更多