Php 类方法并实例化
我正在做一些事情。我试图在config.php中创建一个类,该类包含一个连接到数据库的方法。在dbConnectExample.php中,实例化该类并调用该方法。我有点困惑,因为我也在学习其他编程课程。这就是我目前所拥有的Php 类方法并实例化,php,Php,我正在做一些事情。我试图在config.php中创建一个类,该类包含一个连接到数据库的方法。在dbConnectExample.php中,实例化该类并调用该方法。我有点困惑,因为我也在学习其他编程课程。这就是我目前所拥有的 <?php # Scripts 6.3 - Rectangle.php Class Rectangle { //Declare the attributes: public $width = 0; public $height = 0; //Method
<?php # Scripts 6.3 - Rectangle.php
Class Rectangle {
//Declare the attributes:
public $width = 0;
public $height = 0;
//Method to set the dimensions.
Function set_size($w = 0, $h = 0) {
$this->width = $w;
$this->height = $h;
}
//Method to calculate and return the area.
function get_area() {
return ($this->width * $this->height);
}
// Method to calculate and return the perimeter.
function get_perimeter() {
return ( ( $this->width + $this->height) * 2 );
}
//Method to determine if the rectangle
//is also a square
function is_square() {
if ($this->width == $this->height) {
return true; //Square
} else {
return false; //Not a square
}
}
} //End of Rectangle class.
您可能希望包含一次(“config.php”)
php应该包括1)数据库设置,如用户名和密码,或者2)获取这些值并实际执行连接
首先创建一个包含config.php的php文件
您应该能够从config.php包含的文件中看到“连接到数据库的方法”
一定要打电话。希望有帮助!:)
还要使用new关键字来实例化类,如
$square = new Square();
将数据库连接代码包装到函数中,然后包装到类中。将该类粘贴到config.php中。在dbConnectExample.php中实例化该类(请参阅@jakx中的注释)。您可以创建一个简单的类:
// Database.php
class Database
{
public static function getDbo($dbName)
{
$dbo = new mysqli("localhost", "user", "password");
if (mysqli_connect_errno()) {
throw new Exception(
"<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>"
);
}
if (!$dbo->select_db($dbName)) {
throw new Exception(
"<p>Unable to select the database.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>"
);
}
return $dbo;
}
}
// SomeFile.php
require_once 'Database.php';
try {
$dbo = Database::getDbo();
// If I made it this far then I can start querying
$dbo->query('SELECT stuff FROM table');
// etc.
} catch (Exception $e) {
die($e->getMessage());
}
//Database.php
类数据库
{
公共静态函数getDbo($dbName)
{
$dbo=新的mysqli(“本地主机”、“用户”、“密码”);
if(mysqli\u connect\u errno()){
抛出新异常(
“无法连接到数据库服务器。”
“错误代码”。mysqli\u connect\u errno()
“:”.mysqli\u connect\u error())。“”
);
}
如果(!$dbo->选择_db($dbName)){
抛出新异常(
“无法选择数据库。”
.mysqli_errno($DBConnect)
“:”.mysqli_错误($DBConnect))。“”
);
}
返回$dbo;
}
}
//SomeFile.php
需要_once“Database.php”;
试一试{
$dbo=数据库::getDbo();
//如果我能做到这一点,我就可以开始查询了
$dbo->query('selectstuff-fromtable');
//等等。
}捕获(例外$e){
死亡($e->getMessage());
}
请正确设置代码格式。我说的是缩进。
// Database.php
class Database
{
public static function getDbo($dbName)
{
$dbo = new mysqli("localhost", "user", "password");
if (mysqli_connect_errno()) {
throw new Exception(
"<p>Unable to connect to the database server.</p>"
. "<p>Error code " . mysqli_connect_errno()
. ": " . mysqli_connect_error()) . "</p>"
);
}
if (!$dbo->select_db($dbName)) {
throw new Exception(
"<p>Unable to select the database.</p>"
. "<p>Error code " . mysqli_errno($DBConnect)
. ": " . mysqli_error($DBConnect)) . "</p>"
);
}
return $dbo;
}
}
// SomeFile.php
require_once 'Database.php';
try {
$dbo = Database::getDbo();
// If I made it this far then I can start querying
$dbo->query('SELECT stuff FROM table');
// etc.
} catch (Exception $e) {
die($e->getMessage());
}