简体   繁体   中英

How to read this php code: $input = JFactory::getApplication()->input;

I get confuse reading this php line of code:

$input = JFactory::getApplication()->input;

As I understand this is a declaration for $input variable. Can anyone help to explain the right part of the declaration?

$input = JFactory::getApplication()->input;

JFACTORY is a class

getApplication() is static function and returns JFactory object

input is a parameter

for example

    class JFactory
    {
        public $input='sugumar';
        public static function getApplication()
        {
           return new JFactory;
        }      

    }

    $input = JFactory::getApplication()->input;
    echo $input;

JFactory is a class

JFactory::getApplication() when we use this class is not initialized

but only executes the getApplication(). if you have __construct(){} function it will not be executed. only the getApplication function is executed.

this may assign some value to $input are initialize the class with some default parameter which set $input value... it's bit complicated.

The line of code you are referring to is Joomla's way of reading GET/POST/COOKIE parameters.

$input = JFactory::getApplication()->input;

Let's say you want to get all data received in $_POST. You would do something like:

$input = JFactory::getApplication()->input;
$post_array = $input->getArray($_POST);

If you var_dump($post_array) , you should see the data.

If you want to get specific keys from $_GET, $_POST etc, you would do something like this:

// GET specific dataset
JFactory::getApplication()->input->get->get('username');

// POST specific dataset
JFactory::getApplication()->input->post->get('username');

// COOKIE specific dataset
JFactory::getApplication()->input->cookie->get('usermane');

Now let's get into the meaning of the right hand side:

$input = JFactory::getApplication()->input;

Jfactory is actually the class name. It is called factory because the code architecture for this class is using the Factory design pattern.

In fact, JFactory is the Joomla Platform class, it gives you access to the most important parts in Joomla like the application one or the Joomla global configuration.

getApplication is the method name within the class Jfactory . The fact that it is reference with :: this means that this method is static.

The input is an attribute of the class Jfactory.

You can find more information about the Jfactory class from Joomla documentation .

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