I have a big system that needs to migrated from php5 to php7. I have found tool PHP Code Fixer that helped me finding all the deprecated function and I fixed them all. But there is another thing that causes problem. I have found in the code a lot of people were returning function call in a function, something like this.
function test($array) {
return reset(callSomeArray($array));
}
This will fail as reset function expects a variable as a parameter. In php5 this worked but not in php7. This is correct way:
function test($array) {
$callArray = callSomeArray($array);
return $callArray;
}
Since this is a big software, and I cannot read the all code and test everything. I found out that reset function is causing the problem, but how to find other function that are used the same. Is there some document, or checklist, what to check for migration. Like, list of functions that expect a variable?
Related
I inherited a project that was created with Yii2, ver. 2.0.4, with the task to update said project to a more current version of Yii2 (2.0.15) because of the incompatibility of the older one with PHP 7.2+.
I noticed that there is a lot of use of assigning arrays to a model:
$model->_attributes = $array;
With the new version this results in an exception
'yii\base\UnknownPropertyException' with message 'Setting unknown property: app\models\model::_attributes'
For the time being I created a workaround with the following function:
function customSetAttributes(&$model, $array) {
foreach($model->attributeLabels() as $model_key => $model_label) {
if(!isset($array[$model_key])) continue;
$model->$model_key = $array[$model_key];
}
}
Also, the getter function now has a similar issue.
What I would like to know:
Was this type of assignment never intended in the first place (and I just haven't found the previous developer's code that enables it)? I skimmed over the Yii2 changelog but didn't notice anything related.
Is there a way to "salvage" the previous behaviour so I don't have to replace each occurence with my workaround function?
ActiveRecord::$_attributes was always private and never should be used in this way. I guess that previous developer edited framework core files in vendor directory and make this property protected/public.
You may try to emulate this behavior by creating virtual attribute using getter and setter:
public function get_attributes() {
return $this->getAttributes();
}
public function set_attributes($values) {
$this->setAttributes($values, false);
}
But this will not always work and it is more like an ugly hack to make crappy code work. I strongly suggest to fix code to use setAttributes() instead of _attributes.
Also you should compare yii2 package from vendor directory with source from https://github.com/yiisoft/yii2-framework/releases/tag/2.0.4 - you may find more places where core was edited.
I have read many entries and tried a lot (for example, viewhelpers), but unfortunately nothing works (which is definitely due to my little knowledge). We use the Zend Framework 2 internally with CdliTwoStageSignup (https://github.com/cdli/CdliTwoStageSignup). In this module i want to realize that you can only register if a specific date has not been exceeded. This is currently hardcoded, but should now work with a value from the sql database. This value is already used by other modules, but I do not get it in the module CdliTwoStageSignup. Otherwise, nothing is changed on the module. Unfortunately, I have little idea of php and Zend.
hardcoded looks like this: for submission_deadline a date is manually inserted at the moment
this is from module/CdliTwoStageSignup/view/email-verfication/form.phtml
<?php
if (time() < strtotime($this->submission_deadline)) {
print '<h2>Step 1: <small>Email Verification</small></h2>';
} else {
print '<h2>Step 1: <small>Email Verification (disabled)</small></h2>';
}
Because I have little idea of the framework, I searched in other modules and found that in a view/index.phtml:
<?php
//deadlines
if($this->activeSemester){
$submission_deadline = $this->activeSemester->getDtSubmissionDeadline();
}
Can I use that?
This is a public function in /otherModule/Entity/otherModule.php
public function getDtSubmissionDeadline() {
return $this->dtSubmissionDeadline;
}
So in conclusion is my question: How can I use the variable in this module, what is my problem? I have tried solutions from similar issues, but unfortunately nothing worked. If anything is missing, I'll be happy to complete it.
Thank you very much for your help!
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.
I have been recently playing around with HHVM. Went through a lot of trouble getting it to work on my computer. I know that not all PHP functions are available. As a test, I am writing a new website using it instead of using my current code. I ran into a problem when trying to use
filter_var($var,FILTER_SANITIZE_URL);
From the error.log file, it turns out that this function is undefined. Is the filter_var function not available for use in HHVM or am I just doing something wrong here. I like to keep things DRY, this would mean I have to do a lot more validation than I expected.
filter_var is now implemented in hhvm. Open github issues if you have any problems with it.
This function appears to not have been implemented on HHVM See http://comments.gmane.org/gmane.science.linguistics.wikipedia.technical/70038
An option if you want to rely on this functionality with the hopes that it will enter the fold is to polyfill it in (partial implementation to inspire the motivated).
if (!function_exists("filter_var")){
// define the constants used by the function
define("FILTER_VALIDATE_EMAIL", "email");
function filter_var(){
$args = func_get_args();
// $args[1] is the filter type (second parameter)
switch ($args[1]){
case FILTER_VALIDATE_EMAIL:
if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/", $args[0])?$args[0]:false;
break;
}
}
}
I've found this script for converting wiki syntax to HTML in php and i've tried to integrate it into Codeigniter. it seems really easy to use. However, it's not working, and instead producing around 8 of these errors:
Message: Use of undefined constant LS_NONE - assumed 'LS_NONE'
I think this is because Codeigniter helpers are not a class but rather functions, and this bit of code is a class, or does this issue lie with something else? I've also tried to use it as a model without success.
It also seems horribly outdated (2007). Could somebody suggest a really simple alternative or maybe give an idea of how to convert this to a simple function if that is possible? It's a very short piece of code. I'm not sure how these constants work in relationship to a function versus a class.
I've given the Text_Wiki from Pear ago, but the use and complexity well exceeds both my requirements and knowledge :)
//Any help would be greatly appreciated
Loaded using:
$row = $query->row();
$content=$row->course_content;
$this->load->helper('wiki');
$content=explode("\n", $content);
$output = WikiTextToHTML::convertWikiTextToHTML($content);
$html=array_merge($output);
$data['contents'][]= $html;
$this->load->view('default/a',$data);
It looks like the script is actually a class. put it in the libraries folder and load it with $this->load->library(). That will allow it to properly initialize and define the constants that it uses.
something like:
$this->load->library('wikitexttohtml');
$this->wikitexttohtml->convertWikiTextToHTML($wiki_text);