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.
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)
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();
}
The code:
public function couts_complets($chantier,$ponderation=100){
function ponderation($n)
{
return($n*$ponderation/100); //this is line 86
}
...
}
What I'm trying to do: to declare a function B inside a function A in order to use it as a parameter in
array_map().
My problem: I get an error:
Undefined variable: ponderation [APP\Model\Application.php, line 86]
Try this:
public function couts_complets($chantier,$ponderation=100){
$ponderationfunc = function($n) use ($ponderation)
{
return($n*$ponderation/100);
}
...
$ponderationfunc(123);
}
As of php 5.3 you can use anonymous functions. Your code would look like this (untested code warning):
public function couts_complets($chantier,$ponderation=100) {
array_map($chantier, function ($n) use ($ponderation) {
return($n*$ponderation/100); //this is line 86
}
}
In your current code, $ponderation is not covered by the scope of the function, hence the "undefined" error.
To pass a variable to an "internal" function, use the use statement.
function ponderation($n) use($ponderation) {
Using a callback function:
In order to use a function as a parameter in PHP it is enough to pass the function's name as a string as such:
array_map('my_function_name', $my_array);
If the function is actually a static method in a class you can pass it as a parameter as such:
array_map(array('my_class_name', 'my_method_name'), $my_array);
If the function is actually a non-static method in a class you can pass it as a parameter as such:
array_map(array($my_object, 'my_method_name'), $my_array);
Declaring a callback function:
If you declare in the global space all is good and clear in the world - for everybody.
If you declare it inside another function it will be global but it won't be defined until the parent function runs for the first time and it will trigger an error Cannot redefine function my_callback_function if you run the parent function again.
If you declare it as a lambda function / anonymous function you will need to specify which of the upper level scope variables it is allowed to see/use.
Calling a callback:
function my_api_function($callback_function) {
// PHP 5.4:
$callback_function($parameter1, $parameter2);
// PHP < 5.3:
if(is_string($callback_function)) {
$callback_function($parameter1, $parameter2);
}
if(is_array($callback_function)) {
call_user_func_array($callback_function, array($parameter1, $parameter2));
}
}
This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 8 years ago.
So I'm refactoring my code to implement more OOP. I set up a class to hold page attributes.
class PageAtrributes
{
private $db_connection;
private $page_title;
public function __construct($db_connection)
{
$this->db_connection = $db_connection;
$this->page_title = '';
}
public function get_page_title()
{
return $this->page_title;
}
public function set_page_title($page_title)
{
$this->page_title = $page_title;
}
}
Later on I call the set_page_title() function like so
function page_properties($objPortal) {
$objPage->set_page_title($myrow['title']);
}
When I do I receive the error message:
Call to a member function set_page_title() on a non-object
So what am I missing?
It means that $objPage is not an instance of an object. Can we see the code you used to initialize the variable?
As you expect a specific object type, you can also make use of PHPs type-hinting featureDocs to get the error when your logic is violated:
function page_properties(PageAtrributes $objPortal) {
...
$objPage->set_page_title($myrow['title']);
}
This function will only accept PageAtrributes for the first parameter.
There's an easy way to produce this error:
$joe = null;
$joe->anything();
Will render the error:
Fatal error: Call to a member function anything() on a non-object in /Applications/XAMPP/xamppfiles/htdocs/casMail/dao/server.php on line 23
It would be a lot better if PHP would just say,
Fatal error: Call from Joe is not defined because (a) joe is null or (b) joe does not define anything() in on line <##>.
Usually you have build your class so that $joe is not defined in the constructor or
Either $objPage is not an instance variable OR your are overwriting $objPage with something that is not an instance of class PageAttributes.
It could also mean that when you initialized your object, you may have re-used the object name in another part of your code. Therefore changing it's aspect from an object to a standard variable.
IE
$game = new game;
$game->doGameStuff($gameReturn);
foreach($gameArray as $game)
{
$game['STUFF']; // No longer an object and is now a standard variable pointer for $game.
}
$game->doGameStuff($gameReturn); // Wont work because $game is declared as a standard variable. You need to be careful when using common variable names and were they are declared in your code.
function page_properties($objPortal) {
$objPage->set_page_title($myrow['title']);
}
looks like different names of variables $objPortal vs $objPage
I recommend the accepted answer above. If you are in a pinch, however, you could declare the object as a global within the page_properties function.
$objPage = new PageAtrributes;
function page_properties() {
global $objPage;
$objPage->set_page_title($myrow['title']);
}
I realized that I wasn't passing $objPage into page_properties(). It works fine now.
you can use 'use' in function like bellow example
function page_properties($objPortal) use($objPage){
$objPage->set_page_title($myrow['title']);
}
This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Calling closure assigned to object property directly
If I have a class like this:
class test{
function one(){
$this->two()->func(); //Since $test is returned, why can I not call func()?
}
function two(){
$test = (object) array();
$test->func = function(){
echo 'Does this work?';
};
return $test;
}
}
$new = new test;
$new->one(); //Expecting 'Does this work?'
So my question is, when I call function two from function one, function two returns the $test variable which has a closure function of func() attached to it. Why can I not call that as a chained method?
Edit
I just remembered that this can also be done by using $this->func->__invoke() for anyone that needs that.
Because this is currently a limitation of PHP. What you are doing is logical and should be possible. In fact, you can work around the limitation by writing:
function one(){
call_user_func($this->two()->func);
}
or
function one(){
$f = $this->two()->func;
$f();
}
Stupid, I know.
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