简体   繁体   中英

How to access Joomla protected property?

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?

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..

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM