Passing Object With Reference And Without Reference In PHP

Introduction

This article explains the passing of an object with a reference and the passing of an object without a reference in PHP. This is a small article during which I will attempt to make it clear. First of all we'll explain the object class Hello. In the next example, you'll see that the last line does produce an output. This is often the result of simply passing a string value to a function; a duplicate copy of this output is created within the function. Then within the function we see the string variable, however outside of it, it's still an empty string.

class Hello

{

    protected $world;    

 

    public function __toString()

    {

        return $this->getWorld();

    }

    public function setWorld( $world )

    {

        $this->foo = $world;

    }    

    public function getWorld()

    {

        return $this->foo;

    }

}

Example

<?php

class Hello

{

    protected $world;

       //create magic constant function __tostring 

    public function __toString()

    {

        return $this->getWorld();

    }

    public function setWorld( $world )

    {

        $this->foo = $world;

    }    

    public function getWorld()

    {

        return $this->foo;

    }

}

//object of Hello class

$Hello = new Hello();

$worldVar = '';

//passing the value without reference

function without_refer( $output )

{

    $data = '';  

    if ( $output instanceof Hello )

    {

        $data = 'Object';

        $output->setWorld( 'world' );

    }

    else

    {

        $data  = 'My string';

        $output = 'world';

    } 

    echo $data . ' in the function:<br />';

    echo "output: {$output}<br /><br />";

}

without_refer( $Hello );

echo 'No object in function:<br />';

echo "output: {$Hello}<br /><br />";

without_refer( $worldVar );

echo 'No string in function:<br />';

echo "output: {$worldVar}<br /><br />"

?>


Output

without reference

Now we will explain how we are able to pass a value by reference.

Example

<?php

class Hello

{

// visibility protected

    protected $world;

       //create magic constant function __tostring   

    public function __toString()

    {

        return $this->getWorld();

    }    

    public function setWorld( $world )

    {

        $this->foo = $world;

    }    

    public function getWorld()

    {

        return $this->foo;

    }

}

//object of Hello class

$Hello  = new Hello();

$worldVar = '';

//passing the value with reference use of & sign

function refer( &$output )

{

    $data = '';

    if ( $output instanceof Hello )

    {

        $data = 'Object';

        $output->setWorld( 'world' );

    }

    else

    {

        $data  = 'My string';

        $output = 'world';

    }    

    echo $data . ' in the function:<br />';

    echo "output: {$output}<br /><br />";

}

refer( $Hello );

echo 'No object in function:<br />';

echo "output: {$Hello}<br /><br />";

refer( $worldVar );

echo 'No string in function:<br />';

echo "output: {$worldVar}<br /><br />";

?>
 

Output

with reference


Similar Articles