【问题标题】:How to use a class's method inside a function (PHP)?如何在函数(PHP)中使用类的方法?
【发布时间】:2009-08-26 21:34:38
【问题描述】:

我正在尝试使用新的 PHP mysqli 扩展。我有一个函数 (safe()),它递归地使用 mysql_real_escape_string 来使字符串安全。如何在这个函数中使用我的 mysqli 连接来调用 mysqli::escape_string() 函数?

例子:

$db = new mysqli($host,$user,$password,$database_name);


function safe ($data) {
  if(!is_array($data)) {
     if(!get_magic_quotes_gpc()) {
       $data = **mysqli::escape_string($data)**
       return $data;
    }
  } else {
    return array_map('safe',$data);
  }
}

我在 safe() 里面有 mysqli::escape_string() 我怎么称呼它?在函数之外它将是 $db->e​​scape_string() 但我找不到将其称为 insde 的方法。我尝试将 $db 传递给函数,使 $db 全局等。替代方法是使用程序 mysqli_escape_string() 但这需要将 mysqli 链接资源显式传递给它,但我找不到方法访问它。

【问题讨论】:

  • 你要标记正确的答案吗?

标签: php oop function


【解决方案1】:

将您的数据库对象传递给函数。

 function safe ($data, $db) {
  if(!is_array($data)) {
     if(!get_magic_quotes_gpc()) {
       $data = $db->escape_string($data);
    }
  } else {
    return array_map('safe',$data);
  }
}

【讨论】:

    【解决方案2】:

    我会扩展 mysqli 类:

    class mysqliA extends mysqli{
        function escape_string($data){
            if(!is_array($data)) {
                if(!get_magic_quotes_gpc()) {
                    $data = $this->escape_string($data);
                    return $data;
                }
            } else {
                return $this->escape_string($data);
            }
        }
    }
    

    这样你只需要打电话

    $db = new mysqliA();
    $db->escape_string($data);
    

    【讨论】:

      【解决方案3】:

      我不想鼓励您使用全局变量,但如果您想从安全函数访问$db,您必须将global $db; 放在函数的开头。

      导致:

      $db = new mysqli($host,$user,$password,$database_name);
      
      function safe ($data) {
      
        global $db;
      
        if(!is_array($data)) {
           if(!get_magic_quotes_gpc()) {
             $data = $db->escape_string($data);
             return $data;
          }
        } else {
          return array_map('safe',$data);
        }
      }
      

      请注意,全局变量被认为是邪恶的,不应使用。

      那你应该用什么?好吧,对于您的用例,registry pattern(请记住这一点以备后用)可能最适合。但要让您开始使用面向对象编程,您现在应该尝试以下方法:

      class myClass {
      
          protected $db;
      
          public function __construct() {
              $this->db = new mysqli($host,$user,$password,$database_name);
          }
      
      
          function safe ($data) {
      
            if(!is_array($data)) {
               if(!get_magic_quotes_gpc()) {
                 $data = $this->db->escape_string($data);
                 return $data;
              }
            } else {
              return array_map('safe',$data);
            }
          }
      }
      

      我敦促您阅读有关面向对象编程的更多信息,因为它将帮助您编写更好、更可重用的代码。

      希望我能帮上忙。

      【讨论】:

        【解决方案4】:

        如果我扩展我得到的类:

        Notice: Undefined variable: db in *file path* on line 22
        
        Fatal error: Call to a member function escape_string() on a non-object in *file path* on line 22
        

        第 22 行是函数所在的位置

        如果我传递我得到的 mysqli 对象:

        Warning: Missing argument 2 for safe() in *file path* on line 17
        
        Notice: Undefined variable: db in *file path* on line 22
        
        Fatal error: Call to a member function escape_string() on a non-object in *file path* on line 22
        

        我的函数调用是:

                            $item[$form] = safe($item[$form],$db);
        

        显然没有丢失第二个变量

        而且我无法围绕该函数构建一个类并初始化一个新的 mysqli 连接(无论如何这似乎是低效率的高度),因为我必须将 safe() 设为静态函数以使其成为有效的回调array_map() 并且似乎没有语法适用于这一行:

        $data = $this->db->escape_string($data);
        

        试过

        $data = $this->db->escape_string($data);
        $data = self::db::escape_string($data);
        $data = self::db->escape_string($data);
        

        【讨论】:

        • 致命错误是因为你盲目地复制了代码,如果你看第22行,它说的是$db->而不是$this->
        • 应该是 $db-> 因为我传递的是一个对象,而不是在类本身的上下文中使用它
        • 我的意思是当你扩展课程时
        【解决方案5】:

        在你的 db 类文件中创建一个公共函数

        function escape($string)
            {
                return $this->connection->real_escape_string($string);
        
            }
        

        你可以这样使用它

        function safe()
        
            {
                    $id=$this->mysqli->escape($this->id);
        
                    $status=$this->mysqli->escape($this->status);
        
                    $shortcode=$this->mysqli->escape($this->shortcode);
        
        
        
             }
        

        函数的使用取决于你将如何使用它

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2016-01-30
          • 2012-05-09
          • 1970-01-01
          • 2013-03-08
          • 2011-09-26
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多