How to access Joomla protected property?

2019-09-09 22:31发布

I have tried this and it's working:

<?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);
?>

However, when I tried this:

<?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);
?>

It returns:

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

How can access $connection variable?

3条回答
神经病院院长
2楼-- · 2019-09-09 23:06

This is protected so you need to use a getter to read from, and a setter to write to this property.

Googling for the Joomla API showed up this:

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

Example usage

$conn = $connectorInstance.getConnection();

You also might want to have a look at this article

查看更多
SAY GOODBYE
3楼-- · 2019-09-09 23:16

Try this

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)

Hope this may help you..

查看更多
爷的心禁止访问
4楼-- · 2019-09-09 23:17

You have to use reflection using-php-reflection-to-read-a-protected-property! Read this to learn more about access modifiers stackoverflow or PHP.net

查看更多
登录 后发表回答