how to call two functions from inside a third one - php

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.

Related

“Call to a member function on a non-object” on existing object [duplicate]

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.

Undefined function in 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

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

Can php call non static method without instantiating the class first?

I register class methods for actions in my Wordpress plugin. When my method gets called by Wordpress, if I try to use the $this variable, php throws an error saying the call to $this variable is illegal outside the context of an object.
How can that be? I thought unless the method is static, you're not supposed to be able to call class methods if the class isn't instantiated! My method isn't static! What is happening?
The source code
Obviously the initialize is called from the main plugin file:
add_action('init', array('AffiliateMarketting', 'initialize'), 1);
My class looks like this:
class AffiliateMarketting
{
public function __construct()
{
// some initialization code
}
public function initialize()
{
add_action('woocommerce_before_single_product', array("AffiliateMarketting", "handleAffiliateReferral"));
}
public function handleAffiliateReferral($post)
{
$this->xxx(); // <---- offending function call
}
public function xxx()
{
}
}
The received error message is in fact Fatal error: Using $this when not in object context in <filename> on line <linenumber>.
You have to instantiate the class first. Something like this:
$affiliatemarketing = new AffiliateMarketing;
and then do the following:
add_action('init', array(&$affiliatemarketing, 'initialize'), 1);
Edit: forgot to add, your action in your method should be added like this:
add_action('woocommerce_before_single_product', array(&$this, "handleAffiliateReferral"));
You're not supposed to be. That's why you're getting an error.
I don't know exactly how you're registering the method (code would help), but probably, you're expecting Wordpress to take care of creating an instance, but that's not its role.
I found thought the Codex documented if the class name is specified using its string representation, then the add_action function will assume the call is to a static method.
On the other hand if and instance of the class is passed along then add_action will use that instance to make the method call.
Although Arman hasn't specified which php version he is using, I would assume it's probably 5.3.2 or 5.3.3. The error itself is rather similar to the one described in this question and the solution also would be to upgrade to the latest version of php 5.3.

Categories