我正在使用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