BrownChiLD
BrownChiLD

Reputation: 3733

Passing a variable to its parent function in PHP?

I have a function w/in a function, and I need the inner function to make it's variables available in a scope of parent function, e.g.:

function sayMyName(){
    getName(); // inner function generates $name value

    echo $name; // use $name
}

sayMyName();

I could easily just globalize things w/in both functions... But my situation is far more complicated and handles more variables and globalizing each one is a bit tedious.

Thanks.

PS i noticed a lot of "return" suggestions. sorry i wasnt clear , i need to return more variables.. not a simple return. thanks guys

Upvotes: 1

Views: 1645

Answers (9)

BlitZ
BlitZ

Reputation: 12168

You may use $_GLOBALS, but it`s a "bad practice". So,


1: Use return:

<?php
function getName(){
    $name = 'Smith';

    return $name;
}

function sayMyName(){
    $name = getName();

    echo $name;
}

sayMyName();
?>

Shows:

Smith

2: Use references:

<?php
function getName(&$name){
    $name = 'Smith';
}

function sayMyName(){
    getName($name);

    echo $name;
}

sayMyName();
?>

Shows:

Smith

3: Return array for multiple variables:

<?php
function getName(){
    $surname = 'Smith';
    $name    = 'John';

    return array($surname, $name);
}

function sayMyName(){
    list($surname, $name) = getName();

    echo $name, ' ', $surname;
}

sayMyName();
?>

Shows:

John Smith

4. Return custom object for multiple variables:

<?php
function getName(){
    $surname = 'Smith';
    $name    = 'John';

    $buffer = new stdClass();

    $buffer->name    = $name;
    $buffer->surname = $surname;

    return $buffer;
}

function sayMyName(){
    $obj = getName();

    echo $obj->name, ' ', $obj->surname;
}

sayMyName();
?>

Shows:

John Smith

5. Use anonymous function with use statement and references:

<?php
function sayMyName(){
    $surname = $name = 'Unknown';

    $temp = function() use (&$name, &$surname){
        $surname = 'Smith';
        $name    = 'John';
    };

    $temp();

    echo $name, ' ', $surname;
}

sayMyName();
?>

Shows:

John Smith

Upvotes: 8

Sarrus
Sarrus

Reputation: 631

That what you are thinking is wrong, however you can return an array of values. For ex:

function sayMyName()
{
    $result = getName(); // inner function creates an array

    echo $result['name']; 
}

or better an object:

class Results
{
    public $name;
}

function sayMyName()
{
    $result = getName(); // inner function creating an object

    echo $result->name;
}

Upvotes: 1

Vyktor
Vyktor

Reputation: 21007

This is what the object oriented programming was designed for. If many functions should share variables, it is probably best to encapsulate them to class like this:

class WhateverDescibestYourViewOfTheWorld {
    protected $name;

    function __construct( $name)
    {
        $this->name = $name;
    }

    function GetName() {
        return $this->name;
    }

    function SayName()
    {
        echo $this->name;
    }
}

// And use it:
$o = new WhateverDescibestYourViewOfTheWorld();
...
$o->SayName();

Or you can build class which will be just used as data container:

class DataContainer {
    public $name;
    public $address;
    // ...
}

// By reference this will modify object previously created
function GetProperties( &$dataContainer) // Note that & isn't necessary since PHP5
{
    $dataContainer->name = "...";
}

$c = new DataContainer();
GetProperties($c);

Or you can simplify this and use array:

function GetProperties( array &$dataContainer)
{
     $dataContainer['name'] = '...';
}

$data = array();
GetProperties($data);

Upvotes: 3

Robert
Robert

Reputation: 20286

You can use references

    $param = "aaa";
    function f(&$param)
    {
      //dostuff
      inner($param);
      echo $param;
    }
    function inner(&$inner) { //do other stuff }

or use return value

 function f() { echo inner(); } 

 function inner($param) {return $param;}

if you work on references, both functions will work on same variable, not on a copy

http://php.net/manual/en/language.references.php

the best way would be with class

<?php 
class Person
{
 private $name;

 public function setName($name){ $this->name = $name;}
 public function sayName() {echo $this->name;}
}

$person = new Person();
$person->setName("Robert");
$person->sayName();

It's good way to make it in OOP.

Upvotes: 1

Atanu
Atanu

Reputation: 67

Please use bellow code ,it will solve your problem

global $var;

You can use it anywhere within your php span.

Upvotes: 0

Dipesh Parmar
Dipesh Parmar

Reputation: 27364

You can also do it as below.

$name = "";

function sayMyName(){
    getName(); // inner function generates $name value
    //set $name variable inside getName() function.
    echo $name; // results to undefined
}

sayMyName();

Upvotes: 0

syl.fabre
syl.fabre

Reputation: 746

If you only need one variable you can do this

function getName(){
    // Some code
    return 'my name is XXX';
}
function sayMyName(){
     $name = getName(); // inner function generates $name value
     echo $name; // results to undefined
 }
 sayMyName();

Otherwise you may consider using a class : http://www.php.net/manual/en/language.oop5.php

Upvotes: 1

bwoebi
bwoebi

Reputation: 23787

What about first assigning the return value of getName() to a variable?

$name = getName();

Upvotes: 1

chandresh_cool
chandresh_cool

Reputation: 11830

do this

function sayMyName(){
    $name = getName(); // inner function generates $name value

    echo $name; // results will be returned
}

sayMyName();

I hope your inner function is returning name like this

function getName(){
    return $name;
}

then it will work

Upvotes: 3

Related Questions