如何访问Joomla受保护的财产?

时间:2013-02-20 06:36:35

标签: php database joomla protected

我试过这个并且它正在运作:

<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', dirname(__FILE__).DS."../apitest/");

require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );

$conn = JDatabase::getConnectors();

print_r($conn);
?>

然而,当我尝试这个时:

<?php
define( '_JEXEC', 1 );
define( 'DS', DIRECTORY_SEPARATOR );
define( 'JPATH_BASE', dirname(__FILE__).DS."../apitest/");

require_once( JPATH_BASE . DS . 'includes' . DS . 'defines.php' );
require_once( JPATH_BASE . DS . 'includes' . DS . 'framework.php' );
require_once( JPATH_BASE . DS . 'libraries' . DS . 'joomla' . DS . 'factory.php' );

$conn = JDatabase::$connection;

print_r($conn);
?>

它返回:

Fatal error: Cannot access protected property JDatabase::$connection in C:\xampp\htdocs\apitest1\index.php on line 10

如何访问$ connection变量?

3 个答案:

答案 0 :(得分:1)

试试这个

define( '_JEXEC', 1 );
define('JPATH_BASE', dirname(__FILE__) );//this is when we are in the root
define( 'DS', DIRECTORY_SEPARATOR );

require_once ( JPATH_BASE .DS.'includes'.DS.'defines.php' );
require_once ( JPATH_BASE .DS.'includes'.DS.'framework.php' );

$mainframe =& JFactory::getApplication('site');
$mainframe->initialise();
$db = &JFactory::getDBO(); //Your database object is ready
$sql = "SELECT * FROM #__users";
$db->setQuery($sql);
$db->query();
$res = $db->loadAssocList();
print_r($res)

希望这可以帮到你..

答案 1 :(得分:0)

你必须使用反射using-php-reflection-to-read-a-protected-property!阅读本文以详细了解访问修饰符stackoverflowPHP.net

答案 2 :(得分:0)

这是受保护的,因此您需要使用getter来读取,并使用setter来写入此属性。

Joomla API的Google搜索显示了这一点:

http://docs.joomla.org/API16:JDatabase/getConnection

使用示例

$conn = $connectorInstance.getConnection();

您还可以查看this article