This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 7 years ago.
<?php
class Door
{
public function __construct()
{
}
public function test(){
echo "welocme";
}
}
$obj=new Door();
get_data();
function get_data(){
$obj->test();
}
$obj->test(); work well outside function but i need inside function. I cannot access object inside function show error
Fatal error: Call to a member function test()
Try like this: it may work..
If u use any outer variable in a function, then decleare as global $use_variable_name . now u can understand...
function get_data(){
global $obj;
$obj->test();
}
another and better way:
get_data($obj);// call this way...
function get_data($object){
$object->test();
}
Related
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Reference - What does this error mean in PHP?
(38 answers)
Closed 3 years ago.
I'm new to PHP, i made some research but couldn't find a clear answer.
In a functions.php file i have a function that contains some variables that i need, and a classes.php file containing a class with a method that needs the values from the variables of the function in functions.php.
The Question is: why the echoed variable is outputted but the other variables are called as Undefined.And how can i call and use them.
Example:
functions/functions.php
<?php
// functions.php file
function incFunc(){
$inc_var1 = "Marco";
echo $inc_var1; //this variable output Marco without problem
echo "<br>";
echo "<br>";
$inc_var2 = "Polo"; //this is the variable i want to use in the class method.
}
?>
classes/classes.php
<?php
// classes.php file
include '../functions/functions.php';
class theClass
{
public function classFunc(){
incFunc();
echo $inc_var2; //Notice: Undefined variable: inc_var2
}
}
$obj = new theClass();
$obj->classFunc();
?>
Outputs:
Marco
Notice: Undefined variable: inc_var2 in...
If you want to use variables from incFunc function scope in the class you have to return the value
Example:
function incFunc($returnVar){
$inc_var1 = "Marco";
$inc_var2 = "Polo";
if ($returnVar == 'var1') {
return $inc_var1;
} else {
return $inc_var2;
}
}
Than,in your class method, save the result of a function in an variable:
public function classFunc(){
$inc_var1 = incFunc('var1');
// do stuff
}
This is just a basic example, if you want to include multiple variables at once, you can return an array instead of single variables.
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 6 years ago.
I m searching for some help
It is possible to set a function with Arguments/Parameters inside another function in php? Here i have a theoretical example.
<?php
// Function with parameter/s
function funcOne($arg) {
return $arg;
}
// Parameter/s inside another function.. Possible!?
function funcTwo() {
return funcOne($arg);
}
When i try to set the parameter like this
funcOne('Alex');
echo funcTwo();
I get the following notice error
Notice: Undefined variable: arg in...
Thanks in advance :)
// Function with parameter/s
function funcOne($arg) {
return $arg;
}
// Parameter/s inside another function.. Possible!?
function funcTwo() {
return funcOne($arg);
}
funcOne('Alex');
//Call is made to function one which returned an ARG.
NOTE that here the function just returned the arg and forgot about it, now that argument is NO WHERE stored to be used
//Now here inside the functionTwo scope $arg is never defined.
echo funcTwo();
You may do the following using classes and objects
class MyClass {
public $classarg;
public function funcOne($arg) {
$this->classarg = $arg; //assigned the argument to a class variable
}
function funcTwo() {
return $this->classarg; //using the class variable to test
}
}
$myobj = new MyClass();
$myobj->funcOne('Alex');
echo $myobj->funcTwo()
You can also use global variable to achieve what you want, but I will NOT recommend to use it as Object Oriented Programming is what we should be using going forward
funcOne('Alex')is not setting a parameter, it is calling the function funcOne().
When funcOne($arg) executes, it returns the parameter $arg to the caller.
echo funcOne('Alex') will echo Alex, because that is the value returned.
After return, funcOne does not know about 'Alex' any more.
when you call funcTwo(), it executes funcOne($arg), but $arg is not defined: it has no value assigned.
function funcTwo($arg) {
return funcOne($arg);
}
Note that you should learn to use variables before making functions.
This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 8 years ago.
This is an outline of a problem which I'm struggling to solve in my code. I guess my knowledge of scope isn't that great.. I don't understand why the function getGreeting is giving a parse error.
<?php
class Class_1 {
public $t;
public function __construct() {
$this->t = "hello world";
}
public function helloWorld() {
return $this->t;
}
}
$x = new Class_1();
function getGreeting() {
return $x->helloWorld();;
}
echo getGreeting();
?>
the error I get is:
Fatal error: Call to a member function helloWorld() on a non-object.
Because you need to initialize object in the function to access it's methods from it :
function getGreeting() {
$x = new Class_1();
return $x->helloWorld();;
}
Example
This question already has answers here:
Call to a member function on a non-object [duplicate]
(8 answers)
Closed 10 years ago.
I am can't figure out why this doesn't work:
class Test
{
public static $arData=array();
public static function addMember(Person $member)
{
self::$arData[]=$member;
}
public static function showAll()
{
for($i=0;$i<count(self::$arData);$i++)
{
self::$arData[i]->show();
}
}
}
What I get is this: Fatal error: Call to a member function show() on a non-object.
The show() method does exist and it basically prints out name and location of a person.
In in the constructor, instead of adding $member to $arData I do $member->show() it works.
So... what's up?
Try
self::$arData[$i]->show();
How about this:
foreach (self::$arData as $person) {
$person->show();
}
The error is in the for-loop:
...
public static function showAll()
{
for($i=0;$i<count(self::$arData);$i++)
{
self::$arData[$i]->show();
}
}
...
It must be $i and not only i in the array-access-operator when calling the show()-method.
This question already has answers here:
Calling closure assigned to object property directly
(12 answers)
Closed 9 years ago.
I have php code like:
class Foo {
public $anonFunction;
public function __construct() {
$this->anonFunction = function() {
echo "called";
}
}
}
$foo = new Foo();
//First method
$bar = $foo->anonFunction();
$bar();
//Second method
call_user_func($foo->anonFunction);
//Third method that doesn't work
$foo->anonFunction();
Is there a way in php that I can use the third method to call anonymous functions defined as class properties?
thanks
Not directly. $foo->anonFunction(); does not work because PHP will try to call the method on that object directly. It will not check if there is a property of the name storing a callable. You can intercept the method call though.
Add this to the class definition
public function __call($method, $args) {
if(isset($this->$method) && is_callable($this->$method)) {
return call_user_func_array(
$this->$method,
$args
);
}
}
This technique is also explained in
JavaScript-style object literals