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();
Related
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 have the following code but it does not seem to want to work chained.
$this->view->setData($class_vars);
$this->view->render('addview');
The above works and runs fine but when i try to do the following:
$this->view->setData($class_vars)->render('addview');
I get the following error:
Fatal error: Call to a member function render() on a non-object in....
But the strange thing is when i call it the other way:
$this->view->render('addview')->setData($class_vars);
It runs, but I need the setData to run first as this sets up the var for the actual view, so even though i get the view its got errors where the vars should be? Both methods are public?
Thanks You
Does setData() return the view object (i.e. it has return $this; line)? If not... well it should if you want it to work this way.
For further reference. This technique is called 'fluent interface' and is described here:
http://www.martinfowler.com/bliki/FluentInterface.html
If a variable is holding NULL in PHP, attempting to access a property on it results in the notice "Trying to get property of non-object". If an attempt is made to call a method on it, however, the result is "Fatal error: Call to a member function method-name() on a non-object":
<?php
$obj = NULL;
var_dump($obj->prop);
$obj->method();
http://codepad.org/HZyYd12A
Is there a way to make PHP treat both of these runtime errors as Fatal Errors?
You can register your own error handler, which can promote this kind of error to a fatal error.
See http://php.net/set_error_handler and specifically the examples.
I don't have an idea but i think you can handle it manualy by catching the notice and throwing an exception followed by the line
exit();
;)
I create a subdomain in Wordpress MU and then when I go to that subdomain, I get this error:
Catchable fatal error: Object of class WP_Error could not be converted
to string in /home/pahouse1/public_html/wp-content/mu-plugins/lifetime
/syncronize.php on line 450**
Did anyone face the same issue? What can I do about this?
A bit more code would be nice.
Normally this means you are trying to print an object. Something like this:
$a = new ObjectA();
echo $a;
Which is not possible because, as the error says, a non-string variable (in this case a class object) couldn't be converted to a string.
You can fix this by writing a magic method __toString() for that class.
Full information can be found at http://www.php.net/manual/en/language.oop5.magic.php#language.oop5.magic.tostring.