I have a really serious problem that I have not seen before.
On a website we are using opensource SQC eshop, PHP Version 5.3.3-7+squeeze15 and there is some kind of problem with variable memory I think.
SQC uses notORM and here the problem starts with fatal error "Call to function on non object notORMResult" .
So I dug deeper and found the constructor of NotORM that looks like this:
function __construct(PDO $connection, NotORM_Structure $structure = null,NotORM_Cache $cache = null) {
$this->connection = $connection;
if($_GET['test']){
var_dump($structure);
}
if (!isset($structure)) {
$structure = new NotORM_Structure_Convention;
}
if($_GET['test']){
var_dump($structure);
}
$this->structure = $structure;
if($_GET['test']){
var_dump($this->structure);
exit("1");
}
$this->cache = $cache;
}
And so the output is NULL because the constructor gets no structure param so we create an object. Second output is the object. Then we set the object to attribute and then the THIRD OUTPUT IS NULL
How is this even possible? The site was running for about year and half and no problems till yesterday. I didn't made yet any updates to php and this thing really freaks me out 'cause it's not a constant problem. It just happens sometimes after 2 hours, sometimes after 2 mins and I have really no idea why is this happening.
And btw ... this is just the start it happens across the whole script. Object attributes are set but when you want to read them they give you NULL. There is also second website running on the same server, same php same configuration without problem.
Thanks for any ideas :)
Related
I am facing a very strange Session problem on PHP 8(even though it doesn't work with 7.4 also, but on 7.3 it works great).
What i do is i cerated a class where i set up the Export Object and store it on Session $_SESSION['AjaxExport'][sessionid]. Now i want to load this object stored by reference on the ifram which processes the export. When i session_start it fails to load the session at all.
AjaxExporter Class
private function ExportIntro(){
while (ob_end_clean());
// remove any old ajax exports
unset($_SESSION['AjaxExport']);
$rnd = rand(1, 99999);
$this->sessionid = uniqid($rnd, true);
$_SESSION['AjaxExport'][$this->sessionid] = &$this;
Above, loads the modal where the user gets a link to start the Export. by clicking on that link i load the Below function which loads the Object from Session and start exporting by auto-reloading to export in process like (10%-20%...)
class NG_ADMIN_AJAXEXPORTER_CONTROLLER extends NG_ADMIN_BASE {
public function Export()
{
$sessionid = '';
if (!empty($_REQUEST['exportsess']) && isset($_SESSION['AjaxExport'][$_REQUEST['exportsess']])) {
$sessionid = $_REQUEST['exportsess'];
}
else {
return;
}
$exporter = &$_SESSION['AjaxExport'][$sessionid];
$exporter->sessionid = $sessionid;
$exporter->HandleToDo($_REQUEST['action']);
}
}
When the process tries to start, i get no session at all.
session_start(): Failed to decode session object. Session has been destroyed in...
Also tried to implement Seriazable on both classes but that not seam to work either because, even though the object is stored, there are no values in its properties so it's useless (even though i used reference &).
As i mentioned above, this code works just fine in php 7.3, the problems started since 7.4 and php 8
So, finally figured this out.
As a friend told me that if a class has static properties serialization may not work and i was loading Twig v3 on parent class, so i destroyed all the properties that may contained the "template" property from Twig and it worked.
Generally, cleaned my object to only contain the necessary sub-objects that needed to complete my exports.
I'm trying to build a static code analysis tool and I would like to check if the variables in a file are defined. Currently I'm using nikic/PHP-Parser (https://github.com/nikic/PHP-Parser), but I'm not sure if what I'm attempting is even possible.
So my question is: is it possible to check whether is (possibly) set. So does the variable contain a different value than null? Since the code is not executed in static analysis I feel like it might be impossible to "guess" whether the variable might be null, before giving it to a function for example.
An example:
$page = Expertise::find(get_the_ID());
$relatedNews = $page->connectedNews->take(-3)->reverse();
The second line might give us an exception in this case, when $page turned out to be null. I would like to detect these kinds of instabilities in the code using static analysis.
Here's a piece of code of what I'm attempting using PHP-Parser.
class NodeVisitor extends NodeVisitorAbstract
{
public function enterNode(Node $node)
{
if ($node instanceof Node\Expr\Variable) {
// is not null (is set)
// or if that's not possible: is defined before reference?
}
}
}
Edit: to be more clear on why I'm doing this, I'm trying to build an application that detects possible 500 errors without knowing anything about the execution of the code.
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?
I have the following method I want to test:
class SomeObject {
public function actionFromSomeController() {
$obj = new OtherObject();
$obj -> setAttributes();
$obj -> doAction();
}
}
class OtherObject {
private $_attr;
public function setAttributes() {
$this -> _attr = 'something';
Database :: execute('INSERT INTO table VALUES (' . $this -> _attr . ')');
$fileObj = new FileObj();
$content = $fileObj -> getSomeFileContent();
// do something else
}
public function doAction() {
echo $this -> _attr;
}
}
Now I want to test this method, its output depends on database content and one file on the server. It does a lot of things on the way, and the output is just one ID and success => 1.
How should I test it properly?
Some ideas on how to test small code pieces like this:
Generate test-data and pass it to your methods (also, fake database return data or file contents)
Use echo / var_dump() / die() to check property and variable content at different positions in your methods
Also use these commands to check whether execution reaches a certain point (for example to see whether a function got called or not)
If something doesn't work as expected without an error message: Check line by line with the above methods until you find the problem
Consider using interfaces and dependency injection if your code gets bigger - this is a bit over-the-top for this amount of code, but can be a tremendous time-saver when your application becomes big
Testing is never an automatic process and you will always have to think about what makes sense to do and what not. These things to do are never magic but basic PHP.
You should consider letting your scripts throw errors/exceptions if something goes wrong. Writing "silent" applications is almost never good since you can, if you really need a silent execution for production environments, just turn off error reporting and have the same effect. Many PHP functions return something special on failure and/or success and you can check for this. Database handlers do so, too. Do yourself a favor and use these return values!
I am writing a customer management system in PHP, for use offline (i.e. on the clients computer). I have considered using Java or C# but have come to the conclusion that it is easier to let the browser do all the layout for me, and just have the company install wamp on their computers.
Through this interface they will also be able to manage Agents (i.e. salesmen that go round their area getting orders for the company, in case any doesn't know). This is the section I will use in this post to demonstrate the problem I am having.
Basically I have 4 classes - AgentPages, AgentList, AgentDetails and AgentForm. AgentForm will have two modes - edit and new. AgentPages has a function called getPages, which returns an array of instances of the other 3 classes. However it does not like the "new" keyword.
My code is as follows (for the AgentPages class only):
<?php
require_once("AgentList.php");
require_once("AgentDetails.php");
require_once("AgentForm.php");
class AgentPages {
public function __construct() {
echo "Constructed";
}
private $pages = array("List" => new AgentList(), "Details" => new AgentDetails(), "Form" => new AgentForm());
function getPages() {
return $this->pages;
}
}
?>
I am using the netbeans 6.9 IDE with PHP enabled, and (as you can probably guess) I have wamp server installed. Under PHP version 5.3 the netbeans debugger is telling me that "Parse error: parse error in C:\wamp\www\CustomerApp_v2\Agents\AgentPages.php on line 20". Under 5.2.11 it says something about unexpected T_NEW on that line. I have cut out a large comment on this, before line 20, but I can tell you that line 20 is the declaration of $pages. I have an empty constructor for each class at the moment.
I have also tried the following line instead of line 20:
$AgentList = new AgentList();
This doesnt work either - I get the same error. According to all the tutorials I have looked at there is nothing wrong with my code - I am probably just overlooking something obvious though.
Does anyone have any idea what I am doing wrong? I have done lots of PHP object oriented stuff before now, but the last time I touched it was 2 years ago.
Thanks in advance.
Regards,
Richard
The problem is that you're trying to initialize an instance variable in a declaration with an expression (a call to new is an expression). That doesn't work. Put the assignment in the constructor and it will work.
Like this:
class AgentPages {
public function __construct() {
$this->pages = array("List" => new AgentList(), "Details" => new AgentDetails(), "Form" => new AgentForm());
echo "Constructed";
}
private $pages;
}