I've read several of the threads that already exist with this, or a similar, name. None seems to address my situation exactly.
I'm getting this error from my utils.php class.
Notice: Undefined variable: debug in /app/www/utils.php on line 89 Fatal error: Call to a member function debug() on a non-object in /app/www/utils.php on line 89
I define the variable debug like this:
require_once('PHPDebug.php');
$debug = new PHPDebug();
and then call it (in line 89) like this:
$debug->debug($message);
The reason I'm so baffled is that I copied and pasted these lines from my index.php and that call works just fine.
If you want, I can include links to the index.php and utils.php files, as well as PHPDebug.php.
Thanks to your last comment, the solution to your problem is to declare $debug as global within the function. Thus, you should have something like this:
require_once('PHPDebug.php');
$debug = new PHPDebug();
function myfunction()
{
global $debug;
// some code
$debug->debug($message);
}
You can read more about global in the official doc.
Related
My Try: There is $getSomeData function in a file called bradpitt.php. Its a simple function. Which is not inside a class. Where I have another file name jolie.php. This file is having a class. Where I am trying to access $getSomeData()in that file.
CoolPlugin.php
class CoolPlugin extends plugin
{
const COOLLIST = 'properties/coolBoy.json';
public function getSomeData () {
return DataUtil::readDataFile(self::COOLLIST);
}
bradpitt.php (Non Class File - a simple function)
$getSomeData = function(){
$plugin = new \simulator\CoolPlugin();
return $plugin->getSomeData();
};
jolie.php
include_once 'bradpitt.php';
class Jolie{
public $getSomeData;
public function __construct(){
global $getSomeData;
$this->$getSomeData();
}
}
output.php
include_once 'jolie.php';
$joiliePage = new Jolie();
var_dump($joiliePage->getSomeData);
ERROR:
Notice: Undefined variable: joiliePage in output.php on line 173
Notice: Trying to get property of non-object in output.php on line 173
**NULL**
How to invoke and access a simple function (having a return as an object) inside another class in PHP?
What I doing wrong where it returns NULL?
The code you posted is full of issues.
var_dump($joiliePage->getSomeData);
ERROR:
Notice: Undefined variable: joiliePage in output.php on line 173
Notice: Trying to get property of non-object in output.php on line 173
**NULL**
Assuming is line 173 is the one listed above, both error messages tell the same thing: the variable $joiliePage was not initialized (and the interpreter considers its value is NULL).
Don't get fooled by the fact that PHP classifies them as "Notices". They are notices from the interpreter's point of view (it cannot find a variable) but they are errors for your code as it cannot continue successfully.
include_once 'bradpitt.php';
class Jolie{
public $getSomeData;
public function __contruct(){
global $getSomeData;
$this->$getSomeData()
}
}
The function is called __contruct() but you probably want it to be the class' constructor. It is not the constructor and it is not called automatically by the interpreter because it doesn't have the correct name. The name of the constructor is __construct(). Notice there is an "s" in the middle that is missing in your code.
The method __contruct() declares the global variable $getSomeData. If the file bradpitt.php is successfully included (it may fail with a warning without breaking the script if the file does not exists) then the $getSomeData symbol refers to the variable with the same name defined in file bradpitt.php.
However, the call $this->$getSomeData() doesn't refer to this global variable. It uses the class' property with the same name, which is initialized. This code won't run.
In order to call the function stored in the global variable $getSomeData, the code should read:
public function __construct(){
global $getSomeData;
$getSomeData();
}
Also notice that the statement is missing a semicolon at the end and produces a syntax error. Your class' definition is incorrect, it doesn't compile and objects of type Jolie cannot be created.
I have made a public function for getting, and showing a user's infraction info. When I put it on a page, it only shows what I have in the function, and not any of the other content on the page. It only shows the table headers, and none of the data. I get this error:
Fatal error: Call to a member function query() on a non-object in /Applications/AMPPS/www/classes/user.php on line 108
Also, I have other functions from the same class that work fine.
Here is the code for the function link (sorry about pastebining it, it was really long)
Your $db object is null or can't be accessed. Your line 108 error does not match up with your code you have pasted and you don't have the code where you are creating your database object to see what may be wrong there.
The error message seems to indicate that your "$db" is not set to an object. Make sure its initialized properly.
The function never initializes the $db variable. If it's a class property, it should be $this->db or self::$db. If it's a global variable, you need to put global $db; at the beginning of the function.
before I ask my question, I would like to say that I searched for this question, and none of the other answers helped...
Basically, in my class DemoClass, I have 4 functions, and all of them are "undefined properties"
My error:
Notice: Undefined property: DemoClass::$function in /home/content/92/10270192/html/class.php on line 46
Note: line 46 is where i do $demoClass->function...
I have a typical class setup:
class DemoClass {
public function __construct () {
// stuff that works and gets called
}
public function testFunct () {
// one that is an "undefined property"
}
}
I access the class as normal:
$testClass = new DemoClass();
var_dump(testClass->testFunct); // this is what is on line 46
// ^^^ This also gives me NULL, because its undefined (? i guess...)
I've never had this problem before, any suggestions? Thanks!
Brackets are required when calling a function. Change it to $testClass->testFunct() instead.
$testClass->testFunct references a variable testFunct in the class. You need to use $testClass->testFunct() to reference a function in the class.
It should be
var_dump(testClass->testFunct())
A function always needs the parentheses as else (as you can see) you can't tell the difference between a function and a constant.
Unlike for instance JavaScript, PHP is not handling class methods as regular properties.
When you use $testClass->testFunct, PHP looks for a property named testFunct and finds none.
Methods can be referenced through class name, DemoClass::testFunct in your case.
I get an error that says
Fatal error: Call to undefined method stdClass::mysql_con() in ........../.../includes/script/import.php on line 68.
Line 68 corresponds to:
if(!$ip2c->mysql_con())
I do have a require_once() statement at the beginning of my script
What could be the problem here?
Thanks
Dusoft says it could mean:
$ip2c object does not exist,
Which is not correct because you would get a different error "Fatal error: Call to a member function mysql_con() on a non-object"
He also says it could mean:
mysql_con function is not part of the class you are trying to call
Which is true but not so helpful cos its very difficult to add methods to stdClass.
Additionally it could be to do with serialisation quote:
This error is normally thrown when a class instance has been serialised to disk, then re-read/deserialised in another request but the class definition has not been loaded yet, so PHP creates it as an "stdClass" (standard class.)
Or most likely, I think:
the $ip2c variable was not an object and then php silently cast it to become stdClass somewhere in the code above.
This could happen if you directly assign a property on it.
Like:
$ip2c = null;
//php casts $ip2c to 'stdClass'
$ip2c->foo = bah;
//Fatal error: Call to undefined method stdClass::mysql_con() in...
$ip2c->mysql_con();
See a better example here.
it means that either $ip2c object does not exist or mysql_con function is not part of the class you are trying to call.
I think this happen because "extension=php_mysql.dll" extension isn't loaded in php.ini.
Take a look with
phpinfo();
It could be incorrect code. I once managed to get that error when I had this line of code:
if ($myObj->property_exists('min')){
// do something
}
Which resulted in error line like this:
PHP Fatal error: Call to undefined method stdClass::property_exists() in myFile.php on line ###
I later fixed the line to:
if (property_exists($myObj, 'min')) {
// do something
}
So check for that possibility as well.
Most likely the object does not exist. Please show us the code of how you created it. If you are using it within another class (maybe creating it in the __construct function for example), using:
$ip2c = new Class;
Won't cut it. Instead do:
$this->ip2c = new Class;
and then
$this->ip2c->mysql_con();
I have a class that looks like this:
utils/Result.php
<?php
class Result
{
public static function ok()
{
echo "OK";
}
}
If I create the following script
./sandbox.php
<?php
require_once("utils/Result.php");
print_r(Result::ok());
And run it with php sandbox.php it works fine. But if I do the following: cd test && php ../sandbox.php it gives me the following error
PHP Fatal error: Call to undefined method Result::ok() in /mnt/hgfs/leapback/sandbox.php on line 5
Now, realize that the require statement seems to be working. If I add a property to the Result class, and use print_r on an instance of it, it looks right. But the static methods disappear. I'm very confused. I'm running php 5.2.6.
Do you have a 'utils/Result.php' file in the directory you have changed to (test)? If yes, it will be included instead of the original file.