本文目錄一覽:
- 1、php類的實例化問題
- 2、php中如何在自定義的類中調用pdo中的方法
- 3、求大神 幫將講php中的PDO具體是怎麼用的 , 謝謝了!
- 4、PHP實現的pdo連接資料庫並插入數據功能簡單示例
- 5、php怎麼實例化有依賴注入的類
- 6、php 定義static成員在構造函數中實例化PDO,卻在其他的方法中無法調用PDO的方法???
php類的實例化問題
只要是實例化一個類,不論是不是在同一個文件夾下,只要不是在當前腳本聲明的,都是需要include的. 加入一個對象實例化一個不存在的類,這時候機會調用類的一個魔術方法__autoload方法,參數是你要實例化的這個類名,如果你將這個autoload聲明成了一個方法,方法裡面定義如何查找你需要的這個類,在哪裡查找,並且找到了就自動引入,那就不需要你時時刻刻都來引入類文件了
php中如何在自定義的類中調用pdo中的方法
自定義一個類myPDO,定義一個屬性$pdo,在myPDO的構造方法或者其他方法中,實例化一個pdo對象,賦給myPDO的屬性$pdo
求大神 幫將講php中的PDO具體是怎麼用的 , 謝謝了!
實例化pdo的類
$dsn = “mysql:host=localhost;dbname=test”;
第一個是資料庫類型,第二個是主機地址,第三個是資料庫名稱
$db = new PDO($dsn, ‘root’, ”);
前面的是基本屬性,後面的為用戶名,密碼
這樣就可以進行pdo操作了
PHP實現的pdo連接資料庫並插入數據功能簡單示例
本文實例講述了PHP實現的pdo連接資料庫並插入數據功能。分享給大家供大家參考,具體如下:
創建配置文件
pdo_config.php
?php
$db_Type
=
“mysql”;//資料庫類型
$host
=
“localhost”;//主機名
$dbName
=
“test”;//資料庫名
$userName
=
“root”;//用戶名
$password
=
“root”;//密碼
$dsn
=
“{$db_Type}:host={$host};dbname={$dbName}”;
?
pdo插入資料庫
pdo_insert.php
?php
header(‘Content-type:text/html;
charset=utf-8′);
require
‘pdo_config.php’;
try{
$pdo
=
new
PDO
($dsn,$userName,$password);//創建一個連接對象
$pdo-exec(‘set
names
utf8′);//設置編碼
$sql
=
“INSERT
student
(name,email)
VALUES
(‘李四’,’123@qq.com’)”;
$pdo-exec($sql);
}catch
(PDOException
$e){
die(‘操作失敗’.$e-getMessage());
}
//關閉連接
$pdo
=
null;
?
更多關於PHP相關內容感興趣的讀者可查看本站專題:《PHP基於pdo操作資料庫技巧總結》、《php+mysqli資料庫程序設計技巧總結》、《php面向對象程序設計入門教程》、《php字元串(string)用法總結》、《php+mysql資料庫操作入門教程》及《php常見資料庫操作技巧匯總》
希望本文所述對大家PHP程序設計有所幫助。
您可能感興趣的文章:關於php連接mssql:pdo
odbc
sql
serverPHP5中使用PDO連接資料庫的方法PHP中PDO連接資料庫中各種DNS設置方法小結ThinkPHP框架基於PDO方式連接資料庫操作示例PHP使用ODBC連接資料庫的方法tp5(thinkPHP5)框架連接資料庫的方法示例PHP7使用ODBC連接SQL
Server2008
R2資料庫示例【基於thinkPHP5.1框架】tp5(thinkPHP5)操作mongoDB資料庫的方法thinkPHP5實現資料庫添加內容的方法tp5(thinkPHP5)框架資料庫Db增刪改查常見操作總結PHP利用pdo_odbc實現連接資料庫示例【基於ThinkPHP5.1搭建的項目】
php怎麼實例化有依賴注入的類
PHP依賴注入的理解。分享給大家供大家參考,具體如下:
看Laravel的IoC容器文檔只是介紹實例,但是沒有說原理,之前用MVC框架都沒有在意這個概念,無意中在phalcon的文檔中看到這個詳細的介紹,感覺豁然開朗,複製粘貼過來,主要是好久沒有寫東西了,現在確實很懶變得!
首先,我們假設,我們要開發一個組件命名為SomeComponent。這個組件中現在將要注入一個資料庫連接。
在這個例子中,資料庫連接在component中被創建,這種方法是不切實際的,這樣做的話,我們將不能改變資料庫連接參數及資料庫類型等一些參數。
class SomeComponent {
/**
* The instantiation of the connection is hardcoded inside
* the component so is difficult to replace it externally
* or change its behavior
*/
public function someDbTask()
{
$connection = new Connection(array(
“host” = “localhost”,
“username” = “root”,
“password” = “secret”,
“dbname” = “invo”
));
// …
}
}
$some = new SomeComponent();
$some-someDbTask();
為了解決上面所說的問題,我們需要在使用前創建一個外部連接,並注入到容器中。就目前而言,這看起來是一個很好的解決方案:
class SomeComponent {
protected $_connection;
/**
* Sets the connection externally
*/
public function setConnection($connection)
{
$this-_connection = $connection;
}
public function someDbTask()
{
$connection = $this-_connection;
// …
}
}
$some = new SomeComponent();
//Create the connection
$connection = new Connection(array(
“host” = “localhost”,
“username” = “root”,
“password” = “secret”,
“dbname” = “invo”
));
//Inject the connection in the component
$some-setConnection($connection);
$some-someDbTask();
現在我們來考慮一個問題,我們在應用程序中的不同地方使用此組件,將多次創建資料庫連接。使用一種類似全局註冊表的方式,從這獲得一個資料庫連接實例,而不是使用一次就創建一次。
class Registry
{
/**
* Returns the connection
*/
public static function getConnection()
{
return new Connection(array(
“host” = “localhost”,
“username” = “root”,
“password” = “secret”,
“dbname” = “invo”
));
}
}
class SomeComponent
{
protected $_connection;
/**
* Sets the connection externally
*/
public function setConnection($connection){
$this-_connection = $connection;
}
public function someDbTask()
{
$connection = $this-_connection;
// …
}
}
$some = new SomeComponent();
//Pass the connection defined in the registry
$some-setConnection(Registry::getConnection());
$some-someDbTask();
現在,讓我們來想像一下,我們必須在組件中實現兩個方法,首先需要創建一個新的資料庫連接,第二個總是獲得一個共享連接:
class Registry
{
protected static $_connection;
/**
* Creates a connection
*/
protected static function _createConnection()
{
return new Connection(array(
“host” = “localhost”,
“username” = “root”,
“password” = “secret”,
“dbname” = “invo”
));
}
/**
* Creates a connection only once and returns it
*/
public static function getSharedConnection()
{
if (self::$_connection===null){
$connection = self::_createConnection();
self::$_connection = $connection;
}
return self::$_connection;
}
/**
* Always returns a new connection
*/
public static function getNewConnection()
{
return self::_createConnection();
}
}
class SomeComponent
{
protected $_connection;
/**
* Sets the connection externally
*/
public function setConnection($connection){
$this-_connection = $connection;
}
/**
* This method always needs the shared connection
*/
public function someDbTask()
{
$connection = $this-_connection;
// …
}
/**
* This method always needs a new connection
*/
public function someOtherDbTask($connection)
{
}
}
$some = new SomeComponent();
//This injects the shared connection
$some-setConnection(Registry::getSharedConnection());
$some-someDbTask();
//Here, we always pass a new connection as parameter
$some-someOtherDbTask(Registry::getConnection());
到此為止,我們已經看到了如何使用依賴注入解決我們的問題。不是在代碼內部創建依賴關係,而是讓其作為一個參數傳遞,這使得我們的程序更容易維護,降低程序代碼的耦合度,實現一種松耦合。但是從長遠來看,這種形式的依賴注入也有一些缺點。
例如,如果組件中有較多的依賴關係,我們需要創建多個setter方法傳遞,或創建構造函數進行傳遞。另外,每次使用組件時,都需要創建依賴組件,使代碼維護不太易,我們編寫的代碼可能像這樣:
//Create the dependencies or retrieve them from the registry
$connection = new Connection();
$session = new Session();
$fileSystem = new FileSystem();
$filter = new Filter();
$selector = new Selector();
//Pass them as constructor parameters
$some = new SomeComponent($connection, $session, $fileSystem, $filter, $selector);
// … or using setters
$some-setConnection($connection);
$some-setSession($session);
$some-setFileSystem($fileSystem);
$some-setFilter($filter);
$some-setSelector($selector);
我想,我們不得不在應用程序的許多地方創建這個對象。如果你不需要依賴的組件後,我們又要去代碼注入部分移除構造函數中的參數或者是setter方法。為了解決這個問題,我們再次返回去使用一個全局註冊表來創建組件。但是,在創建對象之前,它增加了一個新的抽象層:
class SomeComponent
{
// …
/**
* Define a factory method to create SomeComponent instances injecting its dependencies
*/
public static function factory()
{
$connection = new Connection();
$session = new Session();
$fileSystem = new FileSystem();
$filter = new Filter();
$selector = new Selector();
return new self($connection, $session, $fileSystem, $filter, $selector);
}
}
這一刻,我們好像回到了問題的開始,我們正在創建組件內部的依賴,我們每次都在修改以及找尋一種解決問題的辦法,但這都不是很好的做法。
一種實用和優雅的來解決這些問題,是使用容器的依賴注入,像我們在前面看到的,容器作為全局註冊表,使用容器的依賴注入做為一種橋樑來解決依賴可以使我們的代碼耦合度更低,很好的降低了組件的複雜性:
class SomeComponent
{
protected $_di;
public function __construct($di)
{
$this-_di = $di;
}
public function someDbTask()
{
// Get the connection service
// Always returns a new connection
$connection = $this-_di-get(‘db’);
}
public function someOtherDbTask()
{
// Get a shared connection service,
// this will return the same connection everytime
$connection = $this-_di-getShared(‘db’);
//This method also requires a input filtering service
$filter = $this-_db-get(‘filter’);
}
}
$di = new Phalcon\DI();
//Register a “db” service in the container
$di-set(‘db’, function(){
return new Connection(array(
“host” = “localhost”,
“username” = “root”,
“password” = “secret”,
“dbname” = “invo”
));
});
//Register a “filter” service in the container
$di-set(‘filter’, function(){
return new Filter();
});
//Register a “session” service in the container
$di-set(‘session’, function(){
return new Session();
});
//Pass the service container as unique parameter
$some = new SomeComponent($di);
$some-someTask();
現在,該組件只有訪問某種service的時候才需要它,如果它不需要,它甚至不初始化,以節約資源。該組件是高度解耦。他們的行為,或者說他們的任何其他方面都不會影響到組件本身。我們的實現辦法
Phalcon\DI 是一個實現了服務的依賴注入功能的組件,它本身也是一個容器。
由於Phalcon高度解耦,Phalcon\DI 是框架用來集成其他組件的必不可少的部分,開發人員也可以使用這個組件依賴注入和管理應用程序中不同類文件的實例。
基本上,這個組件實現了 Inversion of Control 模式。基於此,對象不再以構造函數接收參數或者使用setter的方式來實現注入,而是直接請求服務的依賴注入。這就大大降低了整體程序的複雜性,因為只有一個方法用以獲得所需要的一個組件的依賴關係。
此外,這種模式增強了代碼的可測試性,從而使它不容易出錯。
在容器中註冊服務
框架本身或開發人員都可以註冊服務。當一個組件A要求調用組件B(或它的類的一個實例),可以從容器中請求調用組件B,而不是創建組件B的一個實例。
這種工作方式為我們提供了許多優點:
我們可以更換一個組件,從他們本身或者第三方輕鬆創建。
在組件發布之前,我們可以充分的控制對象的初始化,並對對象進行各種設置。
我們可以使用統一的方式從組件得到一個結構化的全局實例
服務可以通過以下幾種方式注入到容器:
//Create the Dependency Injector Container
$di = new Phalcon\DI();
//By its class name
$di-set(“request”, ‘Phalcon\Http\Request’);
//Using an anonymous function, the instance will lazy loaded
$di-set(“request”, function(){
return new Phalcon\Http\Request();
});
//Registering directly an instance
$di-set(“request”, new Phalcon\Http\Request());
//Using an array definition
$di-set(“request”, array(
“className” = ‘Phalcon\Http\Request’
));
在上面的例子中,當向框架請求訪問一個請求數據時,它將首先確定容器中是否存在這個」reqeust」名稱的服務。
容器會反回一個請求數據的實例,開發人員最終得到他們想要的組件。
在上面示例中的每一種方法都有優缺點,具體使用哪一種,由開發過程中的特定場景來決定的。
用一個字元串來設定一個服務非常簡單,但缺少靈活性。設置服務時,使用數組則提供了更多的靈活性,而且可以使用較複雜的代碼。lambda函數是兩者之間一個很好的平衡,但也可能導致更多的維護管理成本。
Phalcon\DI 提供服務的延遲載入。除非開發人員在注入服務的時候直接實例化一個對象,然後存存儲到容器中。在容器中,通過數組,字元串等方式存儲的服務都將被延遲載入,即只有在請求對象的時候才被初始化。
//Register a service “db” with a class name and its parameters
$di-set(“db”, array(
“className” = “Phalcon\Db\Adapter\Pdo\Mysql”,
“parameters” = array(
“parameter” = array(
“host” = “localhost”,
“username” = “root”,
“password” = “secret”,
“dbname” = “blog”
)
)
));
//Using an anonymous function
$di-set(“db”, function(){
return new Phalcon\Db\Adapter\Pdo\Mysql(array(
“host” = “localhost”,
“username” = “root”,
“password” = “secret”,
“dbname” = “blog”
));
});
以上這兩種服務的註冊方式產生相同的結果。然後,通過數組定義的,在後面需要的時候,你可以修改服務參數:
$di-setParameter(“db”, 0, array(
“host” = “localhost”,
“username” = “root”,
“password” = “secret”
));
從容器中獲得服務的最簡單方式就是使用」get」方法,它將從容器中返回一個新的實例:
$request = $di-get(“request”);
或者通過下面這種魔術方法的形式調用:
$request = $di-getRequest();
Phalcon\DI 同時允許服務重用,為了得到一個已經實例化過的服務,可以使用 getShared() 方法的形式來獲得服務。
具體的 Phalcon\Http\Request 請求示例:
$request = $di-getShared(“request”);
參數還可以在請求的時候通過將一個數組參數傳遞給構造函數的方式:
$component = $di-get(“MyComponent”, array(“some-parameter”, “other”));
php 定義static成員在構造函數中實例化PDO,卻在其他的方法中無法調用PDO的方法???
構造函數在new對象的時候才會調用,如果不調用構造函數的情況下$db就是空對象
你的單例中應該有一個公共的靜態方法吧
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/239457.html