Undefined function in php - php

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

Related

how to call two functions from inside a third one

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.

PHP - accessing a non-class function inside a class

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.

PHP errors Class inside function inside class.. wait what? lost [duplicate]

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.

php i have included a file but cant call functions which are inside class

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

Referencing classes as className::methodName()

As far as I thought you could either instantiate the class as so:
$class = new className();
Then to use a method in it you would just do:
$class->myMethod();
Or if you wanted to use something from within the class WITHOUT instantiating it you could do:
className::myMethod();
I'm sure I have used the latter before without any problems, but then why am I getting an error that says:
Fatal error: Using $this when not in object context
My code I am using to call it is:
// Display lists with error message
manageLists::displayLists($e->getMessage());
The class is as follows..
class manageLists {
/* Constructor */
function __construct() {
$this->db_connection = connect_to_db('main');
}
function displayLists($etext = false, $success = false) {
// Get list data from database
$lists = $this->getLists();
......
}
function getLists() {
.........
}
}
I am getting that error from this line..
$lists = $this->getLists();
When you use the format ClassName::methodName(), you are calling the method 'statically', which means you're calling the method directly on the class definition and not on an instance of the class. Since you can't call static methods from an instance, and $this represents the instance of the class, you get the fatal error.
// Display lists with error message
manageLists::displayLists($e->getMessage());
This is only a valid method of calling instance methods if called from within another instance method of the class. Otherwise, the call to displayLists will be static and there will be no $this reference. If you have a high enough error reporting, you should see a warning telling you you're calling an instance method statically.
If you are calling a method statically, $this does not exist. Instead of:
$lists = $this->getLists();
You can do:
$lists = self::getLists();
More info on self can be found here: When to use self over $this?

Categories