我正在使用Kohana MVC框架。我想添加一個可以與PHPCassa對象(Cassandra NoSQL)一起工作的模塊。在PHP中創建時的空對象
問題是,當我創建對象時,我擁有所有的屬性,我可以在構造中與它交互,但是當我從不同的位置創建對象時,它將返回爲空對象。
我敢肯定,我錯過了一些東西,因爲我剛剛接觸OOP。請幫我在這裏。
模塊文件
<?php
// Loading cassandra libraries, tried to load them here, did not help
//require 'application/modules/cassandra/lib/connection.php';
//require 'application/modules/cassandra/lib/columnfamily.php';
class Cassandra {
function __construct($columnFamily) {
// Loading cassandra libraries
require 'application/modules/cassandra/lib/connection.php';
require 'application/modules/cassandra/lib/columnfamily.php';
$pool = new ConnectionPool('localhost');
$cf = new ColumnFamily($pool, $columnFamily);
// print_r($cf); // This will print object with all the proporties that I can use
return $cf;
}
類,將加載模塊和將創建空對象
<?php
class Controller_Main extends Controller {
public function action_index() {
$a = new Cassandra('timeline');
echo '<pre>';
print_r($a); // This will print out empty Cassandra object
die();
//$this->response->body('hello, world!');
}
}
是的,這工作正常。謝謝。 – sed 2012-04-09 12:52:20
不客氣。請檢查@ cilosis的解決方案,他的方法是如果你的包裝類對象,即Cassandra'object'要在內部操縱對象'ColumnFamily'和'ConnectionPool',你的設計將決定向哪個方向前進。使用單例模式:http://php.net/manual/en/language.oop5.patterns.php如果你要按我的方式 – sakhunzai 2012-04-09 13:04:11