This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Fatal error: Call to a member function query() on a non-object in
I have a class file, lets call it 'stuff'
Stuff.class
inside there is a class
class Stuff {
and in there I have a public static function
public static function morestuff() {
}
Inside I need to call another function for a query
$q="Select from contacts where id =". $this->$db->escapeVal($ID)".";
But I get errors.
$q="Select from contacts where id =". escapeVal($ID)".";
Returns
Call to undefined function escapeVal()
$q="Select from contacts where id =". $db->escapeVal($ID)".";
Returns
Call to a member function escapeVal() on a non-object
$q="Select from contacts where id =". $this->$db->escapeVal($ID)".";
Returns
Using $this when not in object context
So what do I put?
EDIT:
A similar function in the same file has the following code
'id' = '" . $this->db->escapeVal($this->_Id) . "'
However when I try to use this code in my mysql query I get the following error
Using $this when not in object context
I suspect that $db is a global variable, so try
public static function morestuff() {
global $db;
$q="Select from contacts where id =". $db->escapeVal($ID);
}
If the function is in the same class and is also static you will call it like
self::escapeVal($ID);
You are not understanding the concept of OOP. $this is a variable which refers to the current object (or this object). Meaning:
class Test {
public function __construct() { //Called when object is instantiated
var_dump($this);
}
}
$test = new Test();
You'll get something similar to
object(Test)#1 (0) { }
You cannot use $this outside of a class method. It just doesn't work that way.
About the error you're having, try to figure out where is the database connection stored. That connection should either be passed to the object for storing as a field, or directly to the method to work with it internally.
Programming isn't about copy/pasting code that works.
Because your function is static, the variable $this won't exist inside it.
To solve your problem, there are 2 solutions:
Make $db a static variable, meaning it will be the same in every instance.
Remove the static keyword from the function morestuff(). In this case you will need to make an instance of the class in order to make a call to morestuff().
$Stuff->db->escapeVal($id)
started to work. So happy i got it to work, Thank you very one.
Related
I have created a page called User.php, I created inside it three public functions:
public function createUser() {
// to insert the data of the user into a database
}
public function AddRole() {
// to insert the role of the new created user into the same database but of course another table
}
public function register(){ //to call the first two function
$conn = getConn();
createUser();
AddRole();
}
then I called the function register() from another page called registerCtrl.php
$id = $thisUser->register() ;
I am getting this error:
Fatal error: Call to undefined function createUser() in C:\wamp\www\imp\models\User.php on line 114
I think that the problem is in the way I am calling the two functions inside the third one, of course this is only my opinion.
NB: I am new in coding and this is my first software.
thank you guys.
When calling a member function of a class, you need to link it to an instance of that class, in the above case you want to call it on the instance your currently working with, which is this. So the call should be...
$this->createUser() ;
$this->AddRole() ;
Also if $conn is meant to be an instance variable, you also need to prefix it with $this->...
$this->conn = $this->getConn();
(Also depends on where getConn() is defined)
I think the problem is, that you don't have a class. In PHP you can create functions without a class Then you try to acces to the function with an Object.
Try to include the file User.php. require('User.php') or include('User.php') in the registerCtrl.php-File at top. Then just use register();
Fatal error: Call to undefined function createUser() in C:\wamp\www\imp\models\User.php on line 114 -> this means that the Interpreter cant find the function.
This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 9 years ago.
I'm working with a few different classes across different php files. For some reason, the first time I call my function, it works correctly, but the second call results in an error.
This is my "something" class (edited for public view)
class something{
private $db;
function myfunction($sql){
$db = new db();
$results = $db->select("sql query using $sql");
return(empty($results[0]['name'])?0:1);
}
}
"db" is another class I'm using to handle the database queries etc. I don't believe the issue lies in this file since the first call works correctly.
Here's the code I'm using to test it. This is in a different php file from the "something" class.
$something = new something();
echo($something->myfunction('string'));
echo($something->myfunction('string2'));
As I mentioned, the first call ("string") works correctly, but the second one ("string2") fails. This is the error I get.
Call to a member function select() on a non-object
I don't believe it's an issue with the select function (or the db class) since the first call works correctly. I think the issue lies with me declaring $db in "myfunction", but I'm not sure why.
First off, I would shy away from creating a new $db every time you call that function unless you're going to explicity destroy it at the end of the function call.
Secondly, you are creating the class variable private $db but you aren't assigning the db object to it.
You should try the following:
class something{
private $db;
function myfunction($sql){
if(!isset($this->db)){
$this->db = new db();
}
$results = $db->select("sql query using $sql");
return(empty($results[0]['name'])?0:1);
}
}
I feel like that should work better. Let me know!
Edit:
Right- so either do it as my code above shows, or use the class constructor to do it. In PHP the constructor object isn't the class name, it is defined as __construct()
My approach would be either to extend the DB class as I mentioned in my comment above, or to do it in the constructor. My reasoning behind that is because I just don't like creating class-instances inside of methods if I can avoid it, I'd rather make it once when the object is created. SOoooooOOOoOOoOOo:
class something{
private $db;
public function __construct(){
$this->db = new db();
}
function myfunction($sql){
$results = $this->db->select("sql query using $sql");
return(empty($results[0]['name'])?0:1);
}
}
Notice the usage of $this->
That's a PHP thing. Get familiar with it if you aren't- you'll be using it a BUNCH. :)
Good luck!
What kind of DB wrapper are you using? Is database persistence turned on? It migth be disconnecting and not reconnecting the second time round, thus returning an empty DB object.
Can I assume that this is a short version of your code, and you have some logic to instantiate $db only once? In that case you must change that line from
$db = new db();
to
$this->db = new db();
Otherwise db object will be gone at the end of the call.
I wrote three methods in a class and one is calling another, but when I call the function outside through the object, it is showing an undefined function error for the second function.
Here's my code:
function resize_image(){
}
function image_resize(){
$a = resize_image();
}
When I run this, it shows resize_image() as undefined. Here's the error:
Fatal error: Call to undefined function resize_image() in
/home/vacayge/public_html/Major/Alpha1/classes/cUserImages.php on line
2090
Using this you can access the function inside the class
put this code
$a = $this->resize_image();
The thing is that when you call a function that belongs to an object you need to specify which object it belongs to. Similarly if you access a variable that belongs to an object then you need to specify which object it belongs to.
Inside Object
$this->my_func();
$this->my_var = 'foo';
outside Object
$my_obj->my_func();
$my_obj->my_var = 'foo';
Static Stuff(not required by your question but added for completeness)
MyClass::my_func();
MyClass::my_var = 'foo';
$this->resize_image(); this is the way to call a function from outside that function
http://query7.com/using-this-in-php check out for more details
i have included a file in another file and now i want to use on of the functions from the included file.
when i go to the included file and put a variable there the variable get passed and its all good
- that means i have includedd the right file.
but when i want to call a funcion which is inside a class
class SimpleViewer {
.
.
.
.
function whatever(){}
it just doesnt call it when i each it and writes
Fatal error: Call to undefined function display_gallery_table()...
i know for sure im in the right file because the other values passed but i just can call nothing because its insidfe the class
what am i doing wrong?
Try initiating an instance of the class first like this
$myClass = new SimpleViewer();
then call the function like this
$myClass->whatever();
$var = new SimpleViewer();
$var->whatever();
You need to create an instance of the object first;
$obj = new Class();
$obj->func();
There are many ways to call a function between classes. The first I know is a static method call. You can call function by defining class::function().
example :
public class Parent{
public static function check(){
....
....
}
}
the static caller may look alike
Parent::check()
Second way is initiate the class as the new object. From the example we can do the call like
$obj = new Parent;
$obj->check();
the rest manual of implementing this is on php manual
I am using pdo in php and the error i am getting is:
Product sub variant: Product: (
Fatal error: Call to a member function prepare() on a non-object
i have this piece of code in a class and i call it in php, the class is included in the page im calling it.
function get_variantname($variant_id)
{
$query = $db->prepare("select parent_attribute_label from tbl_parent_attribute where parent_attribute_id = :variant_id");
$query->bindParam(":variant_id", $variant_id);
$query->execute();
while($row = $query->fetch(PDO::FETCH_OBJ))
{
return $row->parent_attribute_label;
}
}
You are trying to incorrectly access the $db variable, which looks like a global. Add
global $db;
at the beginning of your function.
The error message tells you that prepare is being called on something that is not an object. But we all know it is an object, since you set it once to some value (there's no chance that it might be e.g. a string). That leaves only the possibility of it not having the value you set inside the function, for some reason. Which leads us to the answer.
You have not passed $db into the function, so it doesn't know what $db->prepare() is supposed to do. If you wish to have global access to your $db class I'd suggest a singleton class. You can also declare $db as a global variable.