Call a function inside a class method and use its variables [duplicate] - php

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.

Related

Access variable from outside function (PHP) [duplicate]

This question already has answers here:
PHP: how to access variables inside a function that have been declared outside of it?
(3 answers)
Closed 1 year ago.
I am trying to access this variable into function but its give me an error Undefined variable: ERROR.
<?php
$ERROR["emptyEmail"] = "empyt email";
$ERROR["emptyPassword"] = "empty password";
function validateLogin($data) {
if (empty($data["email"])) {
return $ERROR["emptyEmail"];
} else if (empty($data["password"])) {
return $ERROR["emptyPassword"];
} else {
return "valid";
}
}
?>
Insert this inside the function:
global $ERROR;
So, the variable can be accessed inside the function scope (see global keyword).
function validateLogin($data) {
global $ERROR;
...
}
Alternatively you can access to all variables that are outside the function using $GLOBALS:
$GLOBALS['ERROR']

Unable to access included variable from separate PHP script [duplicate]

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 not new to programming but I am very new to PHP. I just can't figure out why this variable is not being recognised. I have a file called utils.php in directory utils like this:
<?php
$the_var = 'A'
function foo($bar) {
echo $bar;
}
?>
...and another file called work.php in a parent directory of utils like this:
<?php
include('utils/utils.php');
function doIt() {
echo $the_var; // is always empty
foo('bar'); // no problem
}
?>
Why can't the variable $the_var be accessed?
Variable inside function is not global. If you can access to variable $the_var use
function doIt($the_var) {
echo $the_var;
foo('bar'); // no problem
}
or
function doIt() {
echo $GLOBALS['the_var'];
foo('bar'); // no problem
}

Scope of global array in function [duplicate]

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 trying to add to, and print the contents of a global array that is being accessed within an individual function.
PHP
<?php
// Globals for data cache
$city_array = [];
/* printArray
* print the value of global array
*/
function printArray() {
print_r($city_array);
}
printArray();
?>
This is returning an error:
Notice: Undefined variable: city_array in /Applications/XAMPP/xamppfiles/htdocs/donorsearch/process.php on line 6
How can I get access to this global array within this local function?
To access global variable in function you must use global to tell PHP you want that:
function printArray() {
global $city_array;
....
}
Either use global:
$city_array = [];
function printArray() {
global $city_array
print_r($city_array);
}
printArray();
Pass via function:
function printArray($array) {
print_r($array);
}
$city_array = [];
printArray($city_array);

Passing parameters in a function inside another function? [duplicate]

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.

In php class object not working inside normal function? [duplicate]

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();
}

Categories