phppostgetjoomla3.0

JInput with parameter 'method' as <data-source> results in 'Fatal error: Call to a member function getString() on a non-object'


I'm quite new to Joomla! (and PHP in general) and trying to learn by developing a website on my local Joomla!-Installation. I'm using WAMP-Server with PHP 5.5.12, Apache 2.4.9 and Joomla! 3.6.4.

Now I like to retrieve data from both $_POST and $_GET. Both are equally insecure so it is only logical to retrieve and treat them together.

According to this article https://docs.joomla.org/Secure_coding_guidelines#Secure_strings i should be able to do it like this:

$string = JFactory::getApplication()->input->method->getString( 'myText', '' );

It's not working, complaining that 'method' is a non-object. ('Fatal error: Call to a member function getString() on a non-object')

All other data-source's from that same list (e.g. 'get', 'post', 'cookie', 'request', etc.) do not produce any error and seem to work flawless.

Unfortunately I need to retrieve data from either $_POST or $_GET (or both, but without $_COOKIE) which is exactly what data-source='method' is supposed to do.

Of course I can use 'post' and 'get' sequentially but that seems stupid to me if there is an option which could do it directly (less overhead? and slimmer code). Than I maybe have to address priority, but let's leave that aside for now.

At https://docs.joomla.org/Retrieving_request_data_using_JInput the only Super-Global-'s mentioned are 'get', 'post' and 'server'. Not a word about the other sources that obviously exist (no error occurring) or which of the named sources is used as default.

My search has gone in circles for a while now and I can't find more related information (targeting Joomla! or JInput, not PHP). If I'm missing something fundamental here, feel free to tell me.

With this said my questions are now:

Is there any setting (or update) I have to make to get the 'method'-data-source working?

Is there another value (!='method') for data-source in JInput that can be used to directly retrieve data from exactly either $_POST or $_GET or do I need to sequentially call 'post' and 'get' to accomplish this (maybe 'method' was renamed due to a conflict in names)?

Thanks for your time reading (and maybe answering).


Solution

  • I finally figured it out, thx to @Xorifelse for a push in the right direction.

    The answers to my questions would be:

    1) Is there any setting (or update) i have to make to get the 'method'- working?


    One would have to add an array called '_METHOD' to the $GLOBALS-array in a way like this:

    $GLOBALS['_METHOD'] = array_merge($_GET,$_POST);
    

    Calling the JInput with data-source='method' then would return the merged content of $_GET and $_POST, with $_POST overwriting values in $_GET if they have the same key.


    2) Is there another value (!='method') for data-source in JInput that can be used...?

    Every existing array inside the $GLOBALS-array (who's name starts with an underscore ('_') character and consists of uppercase characters only) can be used by calling the JInput with data-source beeing the name of that array (without the leading underscore).

    If one has newly created and/or filled that array prior to the call by JInput doesn't matter. So using a name like _GETANDPOST instead of _METHOD in the first question would describe its purpose mutch better.


    BUT....

    messing around with $GLOBALS is commonly considered bad practice!!


    For everyone wondering how these values for the data-source in JInput work and why they seemingly could be called like a method even they aren't methods:

    the answer is to be found in 'magic methods' and 'property overloading'.

    https://secure.php.net/manual/en/language.oop5.magic.php

    https://secure.php.net/manual/en/language.oop5.overloading.php#language.oop5.overloading.members