2014-10-29 141 views
-1

我最近在PHP中开始学习面向对象,并且我有这个小代码。 我有这样的代码在class.php:PHP面向对象 - 什么是正确的方法?

## Teacher 
class Teacher 
{ 
    public $_id; 
    public $_name; 
    public $_phone; 

    public function create($id, $name, $phone) { 
     $this->_id = $id; 
     $this->_name = $name; 
     $this->_phone = $phone; 
    } 
} 

## Lesson 
class Lesson 
{ 
    public $_teacher; 
    public $_student; 
    public $_price; 
    public $_date; 

    public function create($teacher,$student,$price,$date) 
    { 
     $this->_teacher = $teacher; 
     $this->_student = $student; 
     $this->_price = $price; 
     $this->_date = $date; 
    } 
} 


mysql.php:

## MySQL Database Connection 

$host = "localhost"; 
$username = "root"; 
$password = ""; 
$dbname = "foo"; 

// Create connection 
$conn = new mysqli($host, $username, $password, $dbname); 

// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} else { 
    echo "Connected successfully"; 
} 

// Create database 
$sql = "CREATE DATABASE $dbname"; 

// sql to create table 
$sql = "CREATE TABLE teachers (
id INT(6) UNSIGNED AUTO_INCREMENT PRIMARY KEY, 
firstname VARCHAR(30) NOT NULL, 
lastname VARCHAR(30) NOT NULL, 
phone VARCHAR(50) 
)"; 

$conn->query($sql); 

,我有这样的index.php中:

include_once('mysql.php'); 
include_once('class.php'); 

## PHP Code 
$teacher1 = new Teacher(); 
$teacher1->create(324,"Ben","054","Audi TT"); 
var_dump($teacher1); 

## PHP Code 
$lesson1 = new Lesson(); 
$lesson1->create($teacher1->_id,date("D d/m/Y H:i"),240,2); 
var_dump($lesson1); 

我每当我在教师o中使用create()方法时,都希望对数据库做一些事情(例如插入) bject。

什么是正确的路要走?

- 我需要在每种方法中创建一个新的mysqli对象吗?

顺便说一句 - 我的代码的一个小审查将是史诗般的。就像,我很想知道我是否有这个权利。

+0

通行证create方法? – developerwjk 2014-10-29 22:45:24

+2

[核心评论](http://codereview.stackexchange.com/)可能更适合这类问题。 – Shoe 2014-10-29 22:47:10

+0

您的意思是添加$ sql到“创建($ id,$ name,$ phone,$ vehicle)”?因为这不起作用(警告:对于Teacher :: create()缺少参数5) – user3800799 2014-10-29 22:48:01

回答

2

可以做些像这样:在`$ conn`

private $connections = array(); 

    public function newConnection($host, $user, $password, $database) 
     { 
      $this->connections[] = new mysqli($host, $user, $password, $database); 
      $connection_id = count($this->connections)-1; 
      if(mysqli_connect_errno()) 
      { 
       trigger_error('Error connecting to host. '.$this->connections[$connection_id]->error, E_USER_ERROR); 
      } 


      return $connection_id; 
     } 

    public function closeConnection() 
     { 
      $this->connections[$this->activeConnection]->close(); 
     } 

     public function insertRecords($table, $data) 
      { 
       // setup some variables for fields and values 
       $fields = ""; 
       $values = ""; 

       // populate them 
       foreach ($data as $f => $v) 
       { 

        $fields .= "`$f`,"; 
        $values .= (is_numeric($v) && (intval($v) == $v)) ? $v."," : "'$v',"; 

       } 

       // remove our trailing , 
       $fields = substr($fields, 0, -1); 
       // remove our trailing , 
       $values = substr($values, 0, -1); 

       $insert = "INSERT INTO $table ({$fields}) VALUES({$values})"; 
       //echo $insert; 
       $this->executeQuery($insert); 
      } 

public function executeQuery($queryStr) 
    { 
     if(!$result = $this->connections[$this->activeConnection]->query($queryStr)) 
     { 
      trigger_error('Error executing query: ' . $queryStr .' - '.$this->connections[$this->activeConnection]->error, E_USER_ERROR); 
     } 
     else 
     { 
      $this->last = $result; 
     } 

    } 
+0

嗨,谢谢。我有一个问题 - 你如何将它与mysql.php上设置的实际连接相连接? excecuteQuery指的是什么? – user3800799 2014-10-29 22:58:43

+1

好的应该现在好吧 – 2014-10-29 23:05:51

+0

谢谢bboy。我是否需要在这两个类中创建此方法? (扩展会解决这个问题,但我不使用所有类中的相同属性) – user3800799 2014-10-29 23:07:15

相关问题