Jump to content

[SOLVED] Passing objects by dereference


SiC_Goat

Recommended Posts

Is there a simple convention to pass an object by its dereference? I'd like something simpler than having to clone the object and then pass it. Furthermore, because this is a recursive method, I'd like to avoid any permanent changes to the data-set I'm looking at while recursing [the data-set is not part of the recursing function, just read and modified at various levels of the recursing; each recursive step should have its own copy of this data-set]

Link to comment
Share on other sites

In a word "eh?".  :o

 

I think you're going to have to be more precise about what it is you're doing. You have a recursive function, you have an object variable (which as of PHP5 are always references), however you seem to be jittering on about maintaining datasets.

 

Perhaps some sample of what you're doing, and what you would like as a result might help further.

Link to comment
Share on other sites

Wow. I actually never knew that everything was passed by reference in PHP.

 

That's dumb.

 

Here's a proof:

<?php

class A {
    public $b;
}


function set_b($obj) { $obj->b = "lol"; }

$a = new A();
$a->b = "before";
$c = $a;

set_b($a);

print $a->b; //i would expect this to show 'before'
print $c->b; //i would ESPECIALLY expect this to show 'before'

?>

Link to comment
Share on other sites

Only objects are passed by reference, and only in php5 (not that anyone should use php4 or earlier). If you want to pass by value you need to use object cloning http://us3.php.net/language.oop5.cloning . To be honest, I like the feature, it saves having to worry about forcing them to be references all the time to save the memory and reduce the execution time on a non-pre-compiled (most of the time) language like php. I suppose it does go against the c++ ideal of "whatever the programmer wants", but meh, simplicity is nice sometimes.

Link to comment
Share on other sites

Yeah, I just read about this.

 

So, in my example, It would look like this:

 

<?php

class A {
    public $b;
}


function set_b($obj) { $obj->b = "lol"; }

$a = new A();
$a->b = "before";
$c = clone $a;

set_b($a);

print $a->b; //i would expect this to show 'before'
print $c->b; //i would ESPECIALLY expect this to show 'before'

?>

 

 

Can I do this?

 

<?php

set_b(clone $a) 

?>

 

I guess I'll try it out.

Link to comment
Share on other sites

Is there a simple convention to pass an object by its dereference? I'd like something simpler than having to clone the object and then pass it. Furthermore, because this is a recursive method, I'd like to avoid any permanent changes to the data-set I'm looking at while recursing [the data-set is not part of the recursing function, just read and modified at various levels of the recursing; each recursive step should have its own copy of this data-set]

 

make it a session variable

Link to comment
Share on other sites

Just as a follow up, I've gone the way of using Clone to facilitate what I need. Making it a session variable would be akin to just passing the same object to the next recurse, no change. Here is a sort of visual of what I'm doing

$b = array ( 'cats' => 1, 'dogs' => 0 );
function a ( $b )
{
if ( $b['cats'] == 1 )
{
	$b_sub = clone $b;
	$b_sub['deer'] = 2;
	a ( $b_sub );
}
}

 

The whole point was that I could modify '$b' in each child recursive tree while retaining its state for future recursing in the current recurse.

 

It's uses are limited, but a near requirement for some of those uses.

Link to comment
Share on other sites

Just as a follow up, I've gone the way of using Clone to facilitate what I need. Making it a session variable would be akin to just passing the same object to the next recurse, no change. Here is a sort of visual of what I'm doing

$b = array ( 'cats' => 1, 'dogs' => 0 );
function a ( $b )
{
if ( $b['cats'] == 1 )
{
	$b_sub = clone $b;
	$b_sub['deer'] = 2;
	a ( $b_sub );
}
}

The whole point was that I could modify '$b' in each child recursive tree while retaining its state for future recursing in the current recurse.

 

It's uses are limited, but a near requirement for some of those uses.

 

u cant call a function from within itself tehre is no scope lol i havent tried but it dosent make sense. What is this witchcraft ?

 

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.