Seems like a simple enough question, so my apologies for asking. As a precursor I'm not necessarily 'new', but rather not so well-versed in PHP.
I have a class, declared as follows:
class User
{
public $id = "";
public function User()
{
$this->$id = isset($_COOKIE['userid']) ? $_COOKIE['userid'] : 0;
}
}
Which seems simple enough, however - upon construction, I get the following set of errors:
Notice: Undefined variable: id in D:\xampp\htdocs\sitecore\include\classes.php on line 13
Fatal error: Cannot access empty property in D:\xampp\htdocs\sitecore\include\classes.php on line 13
Sorry for asking something so simple. The line in question starts with "$this->$id".
Remove the $ symbol at the 'id' place:
$this->id = isset($_COOKIE['userid']) ? $_COOKIE['userid'] : 0;
Related
I get this warning message:
PHP Notice: Only variables should be assigned by reference in /home/sophia/gitter/project/rational-office-2019/tmp/zog.php on line 16
What is in line this 'zog.php' file?
$the_mainrdn = &obtaino($zogcn,'mainrdn');
So, you would think from this warning that the obtaino function would have a second argument that is supposed to be called by reference - but here is the start and end of the function in question:
function obtaino ( $the_xcont, $the_clnom )
{
$awesome_dom = new DOMDocument();
$awesome_dom->loadHTML($the_xcont);
-- <some more stuff here> --
return $awesome_bdv;
}
as you can see, there is no & symbol before the $the_clnom at the opening of the function - so I do not see how this is call by reference.
So what am I missing?
I have a bit of PHP code that works fine on my production server but not on my test server. Here is the code:
function callProcedure0(&$connection, $procname, $dofunction)
{
mysqli_multi_query($connection, "CALL " . $procname . "();");
$first_result = 1;
do
{
$query_result = mysqli_store_result($connection);
if ($first_result)
{
$dofunction($query_result);
$first_result = 0;
}
if ($query_result)
{
mysqli_free_result($query_result);
}
$query_result = NULL;
} while(mysqli_next_result($connection));
}
...
function doGenres($in_result)
{
global $genre_array, $game_array, $genre_order_array;
$genre_count = 1;
// foreach is necessary when retrieving values since gaps may appear in the primary key
while ($genre_row = mysqli_fetch_row($in_result)) // line 81 is here!
{
$genre_array[] = $genre_row[0];
$genre_order_array[$genre_row[1] - 1] = $genre_count;
$game_array[] = [[],[]];
$genre_count += 1;
}
}
...
callProcedure0($con, "get_genres_front", doGenres); // line 138 is here!
The "get_genres_front" bit refers to a stored procedure on my database. Here are the errors:
Notice: Use of undefined constant doGenres - assumed 'doGenres' in /opt/lampp/htdocs/keyboard/keyboard.php on line 138
Again, there are no problems on the production server which is using Apache 2.2.23, MySQL 5.1.73-cll, PHP 5.4.26. The test server where things are broken is running Apache 2.4.10, MySQL 5.6.21, PHP 5.5.19.
Did something change in recent software versions? Thanks.
[edit]
This is not a duplicate question. I'm worried about the first error. I already know what to do about the second error which I have deleted.
The code you have posted is wrong, you must pass function name as string and then use call_user_func to invoke this function.
In your callProcedure0 function change
$dofunction($query_result);
to
call_user_func($dofunction, $query_result);
And then call it with the function name as string like this
callProcedure0($con, "get_genres_front", "doGenres");
The above code could work also with invoking the function with
$dofunction($query_result);
on some php versions, but the line where you pass the function name it should be string, otherwise PHP assumes it is a constant.
I have a model called Treatment in CodeIgniter. I want to load and use this model 'dynamically'. That is, I don't want to have to call it directly by name (I am trying to generalize some code to use whatever model I tell it).
So, I do this:
$namespace = 'blah';
$modelName = 'Treatment';
...
$this->load->model($namespace . '/' . $modelName);
$this->model = $this->$$modelName;
However, I get an error when accessing the $this->$$modelName variable, saying that the variable 'Treatment' is undefined:
Undefined variable: Treatment ... Fatal error: Cannot access empty
property in /mydir/application/controllers/rest/base_rest.php on line
202.
Where line 202 is the line where I am using the $this->$$modelName variable.
Now, if I changed line 202 to be:
$this->model = $this->Treatment;
It works fine.
Does anyone know why I can't seem to use the PHP $$ syntax here?
You can't because it's not a supported syntax. Try
$this->{$modelName}
instead. e.g.
php > class foo { public $bar = 42; }
php > $x = new foo();
php > $y = 'bar';
php > echo $x->$$y;
PHP Notice: Undefined variable: bar in php shell code on line 1
PHP Fatal error: Cannot access empty property in php shell code on line 1
php > echo $x->{$y};
42php >
Notice (8): Use of undefined constant inList - assumed 'inList' [CORE\Cake\Utility\ClassRegistry.php, line 168]
This notice has been bugging me for a while know, and I do not know how to fix it.. It was not really affecting my project earlier since its just a notice msg, but now, it is not letting me show an error message which I am trying to display to the user.
Iv got this function
public function validate_form(){
if($this->RequestHandler->isAjax()){
$this->request->data['Donor'][$this->params['form']['field']] = $this->params['form']['value'];
$this->Donor->set($this->data);
if($this->Donor->validates()){
$this->autoRender = FALSE;
}else{
$error = $this->Donor->validationErrors;
$this->set('error',$error[$this->params['form']['field']]);
}
}
}
The above is the action to which my post request submits to. Then it executes the following to display the error
if (error.length > 0) {
if ($('#name-notEmpty').length == 0) {
$('#DonorName').after('<div id="name-notEmpty" class="error-message">' + error + '</div>');
}
}else{
$('#name-notEmpty').remove();
}
The problem is that instead of the relevant error in my newly created div... I get that notice 8 from cake! Please if anyone knows why this is happening, I appreciate your aid on this one..
TLDR:
Do a project-wide find for 'inList' and find the spot where it either doesn't have quotes around it, or, if it's supposed to be a variable, is missing it's $.
Explanation:
You get that error when you try to use a PHP Constant that doesn't exist. Usually you're not actually TRYING to use a constant, but instead just forgot to wrap quotes around something or forgot to add the $ before a variable.
Examples:
$name = "Dave";
echo name; // <-- WOAH THERE, there is no Constant called name (missing $)
$people = array('Dave' => 'loves pizza');
echo $people[Dave]; // <-- WOAH THERE, no Constant called Dave (missing quotes)
Most likely somewhere else in your code you are using 'inList' as an array key but you don't have it quoted.
Example: $value = $myArray[inList];
It still works without quoting inList but it causes the notice message you're seeing.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to find out where a function is defined?
included php file to know it's file name?
Can I get the filename and line number of the start of a function declaration in PHP?
Say, I have the following code:
1. /**
2. * This function shows the foo bar.
3. */
4. function foo_bar($subject) {
5. echo "Foo bar:\n";
6. if ($subject == "none") {
7. trigger_error("Wrong argument given to 'foo_bar()' in ... on line ....", E_USER_WARNING);
8. }
9. ...
10. }
When I call foo_bar("none"), the function must throw an error like this:
Warning: Wrong argument given to 'foo_bar()' in /home/web/mcemperor on line 4.
Can I retrieve the filename and the line number of start of the function declaration?
You're looking for ReflectionFunction.
$r = new ReflectionFunction('foo_bar');
$file = $r->getFileName();
$startLine = $r->getStartLine();
It's that simple... It works for any defined function, whatever one that you passed in to the constructor argument.
The function name could be found with PHP exceptions, which are more interesting for error triggering : http://www.php.net/manual/en/exception.gettrace.php
$backtrace = debug_backtrace();
$line = $backtrace[0]['line'];
$file = $backtrace[0]['file'];
http://www.php.net/manual/en/function.debug-backtrace.php