Argument value not being passed while creating class object in PHP - php

I am new to PHP and am facing the below issue. There are two files:
In the first PHP file I am writing the below code to create an object
$oNotas = new gcibjdnf($sFiltro, false,'apcconc.nm_apelido', $bBuscarPorChassi);
In the second file-gcibjdnf.php, I have the below code for the constructor
public function __construct($sFiltro = '', $bPaginar = false, $sOrdem= 'apcconc.nm_apelido, danfe', $bBuscarPorChassi = false) {
...}
However,
when I print $sOrdem from the gcibjdnf.php file, I am getting "apcconc.nm_apelido, danfe" as an output.
According to me it should print "apcconc.nm_apelido". But it is printing the default parameter instead of the value passed.
I am using PHP 5.6 version. Please let me know in case anyone has any idea on this.

Related

Check whether variable is defined using nikic/php-parser

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.

PHP variable loses its value

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 :)

Laravel functions arrays and datamodels

I have been working on some old code that was given to me to play with and edit to get familar with and i was wondering if you can see if what i am doing is syntactly sound as im confusing myself with the laravel framework. i basically want to know if im close to what my comments are wanting to do and whether my code and comments marry up or a i missing the point? As im getting the following error message
The error message im getting when running this command through my terminal is
PHP Parse error: syntax error, unexpected '$email' (T_VARIABLE), expecting '(' in /Libraryenter code here/WebServer/Documents/healthandsafetymonitoringsystem.local/app/commands/IncompleteReportsCommand.php on line 157
my code is as follows
// This function uses the parameters $data that is passed to this function each time
// the fire() function loops through and assigns and determines that there is a duereport
private function sendGeneralManagerEmail($data)
{
// create an array called 'Park' that is poulated via the $data parameters
// in the function and is specificly looking for the ParkName entries
$data['Park'] = $Park->ParkName;
//for each item in that array use $ParkName as a key and name each elements $name
foreach($data['Park'] as $ParkName => $name)
{
// Every time i loop through this file get the element $name and
// attach this prefix to it
$email = $name."generalmanager#parkholidays.com";
}
// Then send an email with new $email variable as a reciepient and sending the $data passed
// from the fire() function and used within this functions parameters
Mail::send('emails.GeneralManager', $data, function($message) use $email
{
$message->to( $email, 'General Manager')->subject('[Urgent] Health & Safety Reports');
});
}
Sorry for the dumb question if there are any articles regarding arrays/data models and php functions in laravel that you suggest reading thatll be great. This is a steep learnign curve as i have gone from plain php coding on notepad ++ to laravel 4 and i cant seem to find a nice tutorial that goes from the start to finish with a complete novice in mind.
Regards mike
Regarding your error
You just have a PHP syntax error. Here's the code you want:
// This function uses the parameters $data that is passed to this function each time
// the fire() function loops through and assigns and determines that there is a duereport
private function sendGeneralManagerEmail($data)
{
// create an array called 'Park' that is poulated via the $data parameters
// in the function and is specificly looking for the ParkName entries
$data['Park'] = $Park->ParkName;
//for each item in that array use $ParkName as a key and name each elements $name
foreach($data['Park'] as $ParkName => $name)
{
// Every time i loop through this file get the element $name and
// attach this prefix to it
$email = $name."generalmanager#parkholidays.com";
}
// Then send an email with new $email variable as a reciepient and sending the $data passed
// from the fire() function and used within this functions parameters
Mail::send('emails.GeneralManager', $data, function($message) use ($email)
{
$message->to( $email, 'General Manager')->subject('[Urgent] Health & Safety Reports');
});
}
See the use $email you had that I've changed to use ($email) in the closure at the end.
Regarding your more general question about coding
I can see a few things that don't look right to me. Is this code an exact replica of the function or have you removed some code?
It is hard to say more about you code, since it is taken out of context.
Writing new code as answer, will confuse you more.
So it is better to:
Subscribe to laracast and you will learn everything you need to know about Laravel. Also there is new, updated Laravel 4.1 documentation

When I move code into a function, I get a Fatal error. How can I call code from function

There are two pieces to this code:
One that adds documents to an index to be searched, which works fine, and a crawl() function that is a web-crawler that gets the contents of a page, which also works fine.
But, I need to add a document from inside the crawl() function.
When I move the code that adds a document inside the crawl() function, I get a Fatal Error:
Fatal Error: call to member function addDocument() on a non-object.
I am wondering how I can access the member function addDocument() from inside the crawl function?
Right now, I have a working version where the crawl() function returns what it has crawled in the form of a variable and then the addDocument code, outside the crawl() function, also has access to the returned variable and adds the document to the index that way.
But, that only (logically) works when I am crawling one page or a page with no links to follow. As the function only returns when it is done and since it is recursive to follow a page's links, the only content it will return is the content of the last-page.
Where I need the content of each page to be added each as a new document in the index.
Here is the working code, described above, commented as much as I could: http://pastebin.com/5ngcucDp
and here is the non-working code where I try to move the addDocument() inside the crawl() function: http://pastebin.com/mUEwQJTG
If you have a solution that involves how to access the addDocument() function from inside the crawl() function, then please share.
Or if you have a solution that involves modifying the working code so that it returns the contents of each page it crawls instead of the last-page, please share.
If you have any solutions, please share as I am absolutely exhausted and have tried everything I know.
When moving code to a function, you are completely removing its ability to access variables in the same scope. In this case, you probably (not going to go looking through your off-site code) have something like $someObject = new myClass();, then are trying to access $someObject->addDocument() on it from within the function.
You need to pass $someObject as a parameter to the function, or you could use global $someObject inside the function, though it's not as good an idea.
You have specified that:
// The below line is where the error takes place.
$elasticaType->addDocument($document);
Is your error line. Now, PHP is trying to access a class linked to $elasticaType If you have a linked class then use:
$elasticaType = new ClassName();
If not then you should create a class:
class Name {
public function addDocument ($document){
//Add document code
return $somevar;
}
}
$elasticaType = new Name();
$elasticaType->addDocument($document);

Will empty variables impact my code? (PHP)

In other words, say I have
$existingVariable = 'This is set';
echo thisFunction($existingVariable, $nonExistingVariable);
//included file
function thisFunction($existingVariable){
echo $existingVariable;
}
$nonExistingVariable is no longer there because the included file has changed.
So the way I understand it, $nonExistingVariable would = '' or NULL, right? Does this have any real impact on my code? I'll remove them (or add them back to the included file) before release, but I was just curious if having non-existing variables as an argument risked functionality issues.
It will not affect the function of your code, unless you are using func_get_args(); to work with your arguments instead of just specifying them (you are specifying them, so it won't have any affect)
I.E. you could be doing:
function test() {
$args = func_get_args();
$b = $args[0];
$c = $args[1];
echo "$b\n$c";
}
test('dog','cat');
outputs:
dog
cat
It will affect the readability and user-friendliness of your code moving forward though, as you may try and copy and paste a function call from an old area of code, and get stuck trying to figure out why the variable isn't getting passed into the function (because its no longer an argument).
Why not just remove it, if its not being used anymore?
if the variable doesn't exist then yes PHP will give you an error. "Undefined Variable". You can assign a NULL value, $nonExistingVariable= NULL; Or you can do
if (!empty($nonExistingVariable))
To prevent errors

Categories