SiC_Goat Posted October 8, 2008 Share Posted October 8, 2008 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] Quote Link to comment https://forums.phpfreaks.com/topic/127542-solved-passing-objects-by-dereference/ Share on other sites More sharing options...
aschk Posted October 8, 2008 Share Posted October 8, 2008 In a word "eh?". 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. Quote Link to comment https://forums.phpfreaks.com/topic/127542-solved-passing-objects-by-dereference/#findComment-659997 Share on other sites More sharing options...
keeB Posted October 9, 2008 Share Posted October 9, 2008 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' ?> Quote Link to comment https://forums.phpfreaks.com/topic/127542-solved-passing-objects-by-dereference/#findComment-660600 Share on other sites More sharing options...
genericnumber1 Posted October 9, 2008 Share Posted October 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127542-solved-passing-objects-by-dereference/#findComment-660603 Share on other sites More sharing options...
keeB Posted October 9, 2008 Share Posted October 9, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127542-solved-passing-objects-by-dereference/#findComment-660613 Share on other sites More sharing options...
nadeemshafi9 Posted October 10, 2008 Share Posted October 10, 2008 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 Quote Link to comment https://forums.phpfreaks.com/topic/127542-solved-passing-objects-by-dereference/#findComment-661917 Share on other sites More sharing options...
SiC_Goat Posted October 13, 2008 Author Share Posted October 13, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/127542-solved-passing-objects-by-dereference/#findComment-663920 Share on other sites More sharing options...
nadeemshafi9 Posted October 13, 2008 Share Posted October 13, 2008 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/127542-solved-passing-objects-by-dereference/#findComment-664467 Share on other sites More sharing options...
DarkWater Posted October 13, 2008 Share Posted October 13, 2008 You can call a function from within itself, it's called recursion. Quote Link to comment https://forums.phpfreaks.com/topic/127542-solved-passing-objects-by-dereference/#findComment-664468 Share on other sites More sharing options...
nadeemshafi9 Posted October 14, 2008 Share Posted October 14, 2008 You can call a function from within itself, it's called recursion. i can see that it may be usfull i will keep it in mind. Quote Link to comment https://forums.phpfreaks.com/topic/127542-solved-passing-objects-by-dereference/#findComment-664558 Share on other sites More sharing options...
nadeemshafi9 Posted October 14, 2008 Share Posted October 14, 2008 i have used it myself but didnt rember but yes ok good to see u reminded me, i was mixing variable memory space with function memory space Quote Link to comment https://forums.phpfreaks.com/topic/127542-solved-passing-objects-by-dereference/#findComment-664566 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.