Error passing DOMDocument through functions - php

Im writing a web crawler in PHP and I wrote a re-usable xPath evaluator.The function is:
function xPathEvalSingle($soruce, $xpathExpression) {
$resultsFromXpath = $source->evaluate($xPathExpression)->item(0)->textContent;
return $resultsFromXpath;
}
I create a new DOMXPath object and load from a valid HTML page:
$page = $this->getPageHtml($newCrawlUrl);
$source = new DOMXPath($page);
Then call the function:
xPathEvalSingle($soruce, $xpathExpression) = Fatal error: Call to a member function evaluate() on a non-object
However, when I do it without a function, I get what I want
$resultsFromXpath = $source->evaluate($xPathExpression)->item(0)->textContent = What I want
How would I properly pass the DOMXPath to the function to make it work?

Increase you error reporting level to include notices. You have the same variable typo $soruce in the declaration and the call of the function.
That means in the call an undefined variable is used.
Example:
<?php
error_reporting(E_ALL);
$source = 42;
print($soruce);
Output:
Notice: Undefined variable: soruce in /tmp/... on line 4
Inside the function the argument is not used, but an unknown variable $source.
Example:
<?php
error_reporting(E_ALL);
function foo($soruce) {
print($source);
}
foo(42);
Output:
Notice: Undefined variable: source in /tmp/... on line 5

Related

Fatal error: Call to undefined function tep_session_name()

I am trying to update one of the sites I maintain to the latest PHP and during this, I have come across the following error:
Fatal error: Call to undefined function tep_session_name() in ... /includes/application_top.php on line 83
The code it is referring to is:
// set the session name and save path
tep_session_name('osCAdminID');
tep_session_save_path(SESSION_WRITE_DIRECTORY);
But I have looked at the sessions.php file are the function is defined in the below code:
function tep_session_name($name = '') {
if ($name != '') {
return session_name($name);
} else {
return session_name();
}
}
Any help in identifying the cause would be greatly appreciated.
Thanks, E.
I'm absolutely sure, that you call this function before you include file with function declaration

Call to a member function on a non-object , Undefined variable: login

Ive been trying to get this to work all day but it doesnt seem to work. The $login variable was called but it doesnt affect the if statement:
$login = new Login();
function inclu(){
if ($login->isUserLoggedIn() == true){
include (($_SERVER['DOCUMENT_ROOT']."/project/log/assets/displayuser.php"));
}
else {
// the user is not logged in. you can do whatever you want here.
// for demonstration purposes, we simply show the "you are not logged in" view.
include (($_SERVER['DOCUMENT_ROOT']."/project/log/assets/displaysign.php"));
};
}
It comes up with the errors:
Notice: Undefined variable: login in C:\xampp\htdocs\project\log\index.php on line 38
Fatal error: Call to a member function isUserLoggedIn() on a non-object in C:\xampp\htdocs\project\log\index.php on line
You need to declare $login again in the function:
function inclu() { global $login; /*...*/ }
the first line in the inclu function must be 'global $login;' to get the global variable login accessible inside the function. it's a special scoping technique of PHP

Undefined variable in __construct()... but I did define it

I've written the following piece of code:
Class stackOverflowExample {
private $hash;
private $cookie_file;
public function __construct(){
#session_start();
if(isset($_SESSION['gc_hash'])){
$this->$hash = $_SESSION['gc_hash'];
}else{
$this->$hash = md5(time());
$_SESSION['gc_hash'] = $this->$hash;
}
$this->$cookie_file = "./cookies/{$this->$hash}.txt";
}
}
But I'm getting this error
Notice: Undefined variable: hash in
/var/www/gausie/gc/GeoCaching.Class.php on line 21
Fatal error: Cannot access empty property in
/var/www/gausie/gc/GeoCaching.Class.php on line 21
In the original code, line 21 refers to $this->$hash = $_SESSION['gc_hash'];.
I can't see why this is happening, although I'm new to OO PHP. Any ideas?
just replace $this->$hash by $this->hash
$this->$hash means variable with name equals to variable $hash value

How to handle erroneous argument type in this situation?

I'd like to use stdClass to store options for some methods, instead of passing huge lists of variables (inspired by javascript-style coding)
However, I'd like to make sure I'm always getting an instance of stdClass as an argument. I know I can add a hint in the argument (gb::search below) but when I deliberately try to break it, I'm not sure how to handle the error.
Any tips?
class gb extends CI_Model {
protected $searchtypes = array('full','partial');
protected $endpoint = "https://local.endpoint";
function __construct() {
parent::__construct();
// sample search
$options = new stdClass();
$options->term = 'sample search';
$options->type = 'full';
$this->search($options);
}
function clean_term($term){
$term = trim($term);
return $term;
}
function search(stdClass $options){
$term = $options->term;
$type = $options->type;
// make sure we're doing a valid search
if (!$term || !in_array($type, $this->searchtypes)) {
return false;
}
$term = $this->clean_term($term); // etc
}
The error it throws is something like:
A PHP Error was encountered
Severity: 4096
Message: Argument 1 passed to gb::search() must be an instance of stdClass, null given, called in /application/models/gb.php on line 20 and defined
Filename: models/gb.php
Line Number: 29
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/gb.php
Line Number: 31
A PHP Error was encountered
Severity: Notice
Message: Trying to get property of non-object
Filename: models/gb.php
Line Number: 32
Any ideas how to approach this from a CodeIgniter point of view?
if I remember - mistyped argument should raise E_RECOVERABLE_ERROR, so, it triggers error handler but execution continues. So, you have two options basically.
One is to throw exception in error handler when E_RECOVERABLE_ERROR is encountered. To halt execution.
Another - check type with instanceof stdClass and do what you suppose - raise exception or return something.
UPDATE In your case your framework (CI is for CodeIgniter?) sets error handler (somewhere using set_error_handler). So, after logging or printing error message execution continues. (If there was not handler you would get fatal error). Just test type of argument manually:
function search(stdClass $options){
// test type for sure, because of recoverable error
if (!($options instanceof stdClass)) {
return false; // or throw new InvalidArgumentException('Parameter should be instance of stdClass');
}
$term = $options->term;
$type = $options->type;
// make sure we're doing a valid search
if (!$term || !in_array($type, $this->searchtypes)) {
return false;
}
$term = $this->clean_term($term); // etc
}

Error defining variable in PHP object

I am getting the following error in the following code:
Class primeField implements field {
private $intmodulus = '';
public function generator(){
return ;
}
public function modulus(){
return $this->$intmodulus;
}
public function __construct($modulus , $base=0) {
if (is_resource($modulus) && get_resource_type($modulus) == "GMP integer"){
$this->$intmodulus = $modulus;
} else{
$this->$intmodulus = gmp_init($modulus , $base); \\line 70
}
}
}
$a = new primeField(11);
$a->modulus();
Notice: Undefined variable: intmodulus in /Users/admin/PHP ECC/finitefield.php on line 70
Fatal error: Cannot access empty property in /Users/admin/PHP ECC/finitefield.php on line 70
Why
The syntax is
$this->intmodulus
not $this->$intmodulus.
You get an error saying "cannot access empty property" because $intmodulus is undefined and hence accessing it gives NULL. The NULL gets converted into an empty string when you attempt to use it as a property name.
If the value of $intmodulus was the name of a valid property (e.g. if $intmodulus == "intmodulus"), you would be accessing the property with that name.
$this->$intmodulus should be $this->intmodulus
See the PHP documentation for Variable variables for info on what is happening.

Categories