Different Methods in PHP Classes depending on server? - php

I wrote a little PHP Blogscript with classes but ended in a problem. i have 2 webspaces and on 1 it works, on the other it doesn't.
i have defined a class called $system and within this class there is a function called the_querystring. it just returns an array with all the exploded querystring entries.
<?php $system->the_querystring()['variable']; ?>
my problem is - on 1 server it works fine, on my other server i have to write
<?php $system->the_querystring(['variable']); ?>
Any idea why?

According to php.net the notation
$secondElement = getArray()[1];
is allowed from PHP 5.4
Maybe your webserver has a PHP version older, something like
$querystring = $system->the_querystring();
$querystring['variable'];
should work on both.

Related

Cakephp 3 Interacive Component Action

I change with cakephp 3.2 to a new server and this server is running php 7 instead of 5.4. Now I have an issue with my interactive way of calling an component and the needed action. What I was using is the following:
$data[$csvKnowField->field_number] = $this->Replace->$csvKnowField['imports_mapping']['component_action']($data[$csvKnowField->field_number]);
This is giving the error: Function name must be a string. As far as I can see the problem is coming from the action called because if I change it to:
$data[$csvKnowField->field_number] = $this->Replace->replaceComma($data[$csvKnowField->field_number]);
everything works fine. The variable $csvKnowField['imports_mapping']['component_action'] is holding an interactive value so the different actions can be called in the Replace component, so it would be great to keep it that way.
Is there somebody how knows an solution so the the value in the variable can be used as an interactive way?

Joomla:Why use JView/assignRef() instead of just assigning the value

I came across this question: Whats is assignRef() function in joomla 2.5 when I tried to search for definition of this assignRef() function.
Upon reading the answers, I still have a question:
It seems to me this function is used to create a key for the object and assign a value to it, like
$this->assignRef('messages', $messages)
If that is right, then why don't just do $this->messages = $messages?
I think these anwsers anwser your question.
Whats is assignRef() function in joomla 2.5
Joomla 3 - What to use instead of assignRef?
According to above answers it is a php related thing.
Older version of Joomla(1.5) used a PHP version which is < PHP 5.2
Newer version of Joomla(2.5) used a PHP version which is > PHP 5.2
In older version of php when you assign a variable like this
$this->messages= $messagesOrig;
php creates a copy of object $messagesOrig variable is referring and assign it to $this->messages.
Meaning any changes you are doing using $this->messages will not affect original object (the one $messagesOrig was referring).
So you have to use assignRef() and you will be using/Editing the original object as $messagesOrig refering
$this->assignRef('messages', $messagesOrig)
In the newer version of php by default php assign a reference to orginal object.So you dont need to use assignRef()
References:
Joomla 3 - What to use instead of assignRef?
https://docs.joomla.org/API17:JView::assignRef
PHP References Explained
JView[Legacy]::assign() and JView[Legacy]::assignRef() have been deprecated in Joomla!3 in favor for native PHP syntax.

PHP Functions from included files don't execute on Web Server

I am in the process of migrating a site from my personal dev server onto Windstream's business hosting server. I've already run into the issue of having developed using PHP 5.4 only to find out that my static functions won't work on WS's 5.1.4 installation. I've since fixed those issues and am not facing one that I can't seem to find any help for on the internet.
All of the static functions I was using have been rewritten as functions outside the class scope. Instead of having
class Product{
...
public static function myFunction(){}
...
}
I now have
function myFunction(){}
class Product{...}
in my included Product.php file.
However, when I try to call myFunction() from my code, nothing happens. I know the nothingness comes from WS's error handling, but the point is, the function isn't working. To verify this, I have taken the following steps:
Inserted the line echo "entered included"; immediately following the <?php in Product.php. This prints "entered included" on the index page, indicating that my include is working. I have done the same thing before the final ?> with the same results, so I don't think it's getting hung up inside the included file.
I have changed myFunction() in the included file to be simply
function myFunction(){echo "myFunction works";}
A call to myFunction() still makes nothing happen.
I have moved myFunction() to the including file (myFunction() now lives in index.php instead of Product.php; index includes Product.php). This time, myFunction() executes without issue.
To my 'hack it til it does what it should' sensibilities, this tells me that the server is having a problem with functions that are declared in files that are included; honestly, though, I have absolutely no clue what's going on, and any help would be appreciated. Their website is currently down, and they were expecting it to only be offline for a day, so I'll try pretty much anything short of sacrificing a fatted calf.
I know I should be posting more specific code, but since this is a customer's actual website, I'm trying to put as little of the actual code out here as is possible. I'm happy to append specific sections of code to this entry as they are requested for clarification.
Thanks in advance for your consideration.
#Rottingham: First, thanks for the 3v4l link. Second, my assumption about static methods in 5.4 vs 5.1.4 came from this line of php.net's page on static members and methods:
"As of PHP 5.3.0, it's possible to reference the class using a variable. The variable's value can not be a keyword (e.g. self, parent and static)."
src - http://www.php.net/manual/en/language.oop5.static.php
Since my version and the server version were on different sides of the 5.3 mark mentioned, I incorrectly assumed that this was my problem.
Third, when I get in from my day job, I'll update my code to show errors and update this post if a solution has not yet been found.
Ultimately, my problem isn't with using static methods (since I don't have them anymore) but with using any function that is declared in an included .php file.

Find double defined functions in PHP

I work in a php project with multiple independent developers and recently we had case where a function getmicrotime() was twice defined.
all worked fine, because they were defined in different files that were not both included in a single call ... until some refactory.
in the standardcase php would just output a fatal error, but here the output was blocked. (because a thirdparty website called a website ...) so we did not get the output, just the information that nothing worked anymore.
To the point:
Is there any method, external script, etc to check if functions with the same name are defined twice in the project?
i thought about reg. expr search, but ofcourse class methods can have the same name like a::meth1 and b:meth1 .... so its not that easy.
i am talking about a project with ~100.000 lines of ugly code ... so manual checking is not possible
Thanks in advance.
Consider static code analysis. I would suggest Sonar + PHP plugin: http://docs.codehaus.org/display/SONAR/PHP+Plugin
Here is the life example how it works:
http://nemo.sonarqube.org/dashboard/index/net.php.pear.phpcodesniffer
You can always write a simple script (i.e. perl or python) which will find all duplicates. The algorithm would be simple...

Issues with Searchable Behavior and PHP 4.x.x

I am trying to use Searchable Behaviour hosted here: http://code.google.com/p/searchable-behaviour-for-cakephp/
To initialize the search_index table with existing data, I have used this: http://code.google.com/p/searchable-behaviour-for-cakephp/issues/detail?id=1&q=controller Reply No. 2 which creates the class SearchController.
The code was working fine on my local test server which runs PHP5.
However, the server has PHP 4 and when I tried the code on server it gives this error:
Fatal error: Cannot redeclare class searchcontroller in LONG_PATH/cake/app/models/behaviors/searchable.php on line 2
Does it really has to do something with the PHP version or have I done some logical mistake?
Not sure, but you could try my Searchable plugin instead
I just changed the name of the search Initialization controller and it worked. Looks like there is a Search Class created with Searchable behaviour.
Also, while using searchable plugin for CakePHP 4.x.x, if html_entity_decode and iconv are giving problems(as they did to me), use ut8_decode for decoding in place of html_en... and just comment out the iconv. The result may look ugly, but it works for most cases.

Categories