面向对象的问题

时间:2010-12-27 23:39:08

标签: php

我试图将所有数据库连接放在1个php文件中,而不是放在每个单独的php页面中。我有以下内容:

// conn.php:

<?php

class conn {
   var $username = "name";
   var $password = "password";
   var $server = "localhost";
   var $port = "3306";
   var $databasename = "db";
   var $tablename = "tablename";
   var $connection;

   public function getConnected() {

       $this->connection = mysqli_connect(
       $this->server,
       $this->username,
       $this->password,
       $this->databasename,
       $this->port
        );
}


}
?>

// file.php:

<?php
require_once("conn.php");

class myClass{

public function con() {

$conn = new conn();
$conn->getConnected();    
}

public function myF() {
$stmt = mysqli_prepare($conn->connection, "SELECT * FROM $conn->tablename");

mysqli_stmt_execute($stmt);
    }
}
?>

然后我将其称为:

$ myNew = new myClass(); $ myNew-&GT; CON(); $ myNew-&GT; MYF();

当我这样称呼时,我收到以下错误:

未定义的属性:myClass :: $ connection

我做错了什么?

6 个答案:

答案 0 :(得分:0)

我认为您对错误消息略有不妥。 我怀疑你实际看到的错误是:

Undefined property: myClass::$conn

如果这是真的,那很清楚。 $conn仅在con()函数中是本地的,并且在myF()函数中不存在。

您应将$conn声明为myClass的属性。

答案 1 :(得分:-1)

myF无权访问$ conn或连接

1。)在Conn中声明连接公共

class MyConn {
  private static $conn; //it should be static since it only exists once

  public __construct() {
    self::$conn = new Conn()->getConnected();
  }

  public myF() {
    $stmt = mysqli_prepare(self::$conn->connection, "SELECT * FROM self::$conn->tablename");
    mysqli_stmt_execute($stmt);
  }
}

好的......根据你这样做的理由 - &gt;我会在哈希中创建一个信息数组,如:

class MyConn {
  private $connections = array(
    "blog" => array(
       "username" => "name",
       "password" => "password",
       "server" = "localhost",
       "port" => "3306",
       "databasename" => "db",
       "tablename" => "blog"
    ),
    "admin" => array(
       "username" => "name2",
       "password" => "password2",
       "server" = "localhost",
       "port" => "3306",
       "databasename" => "db",
       "tablename" => "admin"
    )
  );

  private static $active_connection;

  public __construct($connection_name = NULL) {
    if(in_array($connection_name, $this->connections)) {
      self::$active_connection = mysqli_connect($this->connections[$connection_name]);
    } else {
      throw new Exception("That is not a valid connection name!");
    }
  }

  public conn() {
    return self::$active_connection;
  }
}

现在你可以这样做:

$blog_conn = new MyConn('blog');

答案 2 :(得分:-1)

myClass中,您在con()中连接,但$conn是该方法的本地变量;即班级本身无法访问它。要解决这个问题,

class myClass {

    public $conn;

    public function con() {
        $this->conn = new conn();
        $this->conn->getConnected();    
    }

    public function myF() {
        $stmt = mysqli_prepare($this->conn->connection, "SELECT * FROM {$this->conn->tablename}");
        mysqli_stmt_execute($stmt);
    }

}

答案 3 :(得分:-1)

$conn仅在public function con()范围内声明。首先在myClass中声明它,看看会发生什么。

答案 4 :(得分:-1)

我不认为该变量是公开的。您应该专门将其公开,或者创建一个返回连接的函数。

答案 5 :(得分:-1)

您应该向“MyClass”添加一个字段,该字段将指针存储到方法之间的连接。

另一个有用的东西: 不要使用“var”,它在php5中已弃用,只是为了向后兼容而等于“public”。 尽量不要在你的课程中使用公共字段,这在oop编程中被认为是一种糟糕的策略(google encapsulation)。 您可以猜测,如果您创建另一个MyClass实例,并且为此实例调用“con()”方法,则将有2个已打开的数据库连接,这对性能非常不利。解决方案是单例模式(google singleton,有一些php示例)