Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/oop/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP面向对象-正确的方法是什么?_Php_Oop_Object_Mysqli - Fatal编程技术网

PHP面向对象-正确的方法是什么?

PHP面向对象-正确的方法是什么?,php,oop,object,mysqli,Php,Oop,Object,Mysqli,我最近开始学习PHP中的面向对象,我有这个小代码。 我在class.php中有以下代码: ## Teacher class Teacher { public $_id; public $_name; public $_phone; public function create($id, $name, $phone) { $this->_id = $id; $this->_name = $name; $t

我最近开始学习PHP中的面向对象,我有这个小代码。 我在class.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);
每当我在teacher对象中使用create()方法时,我都想对数据库做一些事情(例如insert)

正确的道路是什么

-是否需要在每个方法中创建新的mysqli对象?


顺便说一句,对我的代码进行一个小的回顾将是史诗般的。比如,我很想知道我是否做对了。

你可以这样做:

    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;
        }

    }

是否将
$conn
传递给create方法?可能更适合此类问题。您的意思是将$sql添加到“create($id、$name、$phone、$vehicle)”中?因为这不起作用(警告:缺少Teacher::create()的参数5),顺便说一句,感谢投了反对票的人。我相信他是一个非常聪明的婴儿,会OOP。对于插入使用嗨,谢谢。我有一个问题-如何将它与mysql.php上设置的实际连接连接起来?excecuteQuery指的是什么?好的,现在应该很好了,谢谢你。我是否需要在两个类中都创建此方法?(extend可以解决这个问题,但我不会在所有类中都使用相同的属性)为了保持简单,您可以复制并粘贴所需的属性。为了学习面向对象设计的原则,我推荐这本书,我目前正在阅读这本书,我将为此而努力。这对我帮助很大。它现在有4个版本,共有100多篇评论,大多数评论都是正面的。