<?xml version='1.0' encoding='UTF-8'?><?xml-stylesheet href="http://www.blogger.com/styles/atom.css" type="text/css"?><feed xmlns='http://www.w3.org/2005/Atom' xmlns:openSearch='http://a9.com/-/spec/opensearchrss/1.0/' xmlns:georss='http://www.georss.org/georss' xmlns:gd='http://schemas.google.com/g/2005' xmlns:thr='http://purl.org/syndication/thread/1.0'><id>tag:blogger.com,1999:blog-8574664069419810927</id><updated>2011-10-06T20:14:38.665+01:00</updated><category term='php php4 php5 object references POG'/><title type='text'>Sportsdaft</title><subtitle type='html'></subtitle><link rel='http://schemas.google.com/g/2005#feed' type='application/atom+xml' href='http://sportsdaft.blogspot.com/feeds/posts/default'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8574664069419810927/posts/default?max-results=100'/><link rel='alternate' type='text/html' href='http://sportsdaft.blogspot.com/'/><link rel='hub' href='http://pubsubhubbub.appspot.com/'/><author><name>Andy Law</name><uri>http://www.blogger.com/profile/16193067228538333296</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><generator version='7.00' uri='http://www.blogger.com'>Blogger</generator><openSearch:totalResults>2</openSearch:totalResults><openSearch:startIndex>1</openSearch:startIndex><openSearch:itemsPerPage>100</openSearch:itemsPerPage><entry><id>tag:blogger.com,1999:blog-8574664069419810927.post-5188129361069779322</id><published>2008-04-17T16:15:00.011+01:00</published><updated>2008-04-17T20:47:10.957+01:00</updated><category scheme='http://www.blogger.com/atom/ns#' term='php php4 php5 object references POG'/><title type='text'>By reference vs. By value in php</title><content type='html'>&lt;h2&gt;Variables and references&lt;/h2&gt;In a &lt;a href="http://groups.google.com/group/Php-Object-Generator/browse_thread/thread/9219109b89b87f1f"&gt;recent post&lt;/a&gt; to the PHP Object Generator mailing list, John asked a question about the significance of the &lt;span style="font-weight: bold;"&gt;&amp;amp;&lt;/span&gt; in front of the &lt;span style="font-weight: bold;"&gt;$variable&lt;/span&gt; name when passing objects into routines in POG.&lt;br /&gt;&lt;br /&gt;Since this has come up in a few fora that I keep tabs on, as well as in my own coding, I thought that I'd have a go at explaining it.&lt;br /&gt;&lt;br /&gt;As with all my thoughts and replies, I reserve the right to be utterly wrong, so &lt;span style="font-style: italic;"&gt;caveat lector&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;When assigning variables from one place to another, there are two possible meanings behind the assignment. The first, which seems to be the obvious interpretation to the majority of people is that assigning variable A to variable B implicitly means 'take the value stored in variable A and put it into variable B'.&lt;br /&gt;&lt;br /&gt;Thus, in php, if we define &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; as containing 'abc', then assign &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; to &lt;span style="font-weight: bold;"&gt;$b&lt;/span&gt;, we expect that both &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;$b&lt;/span&gt; will contain 'abc'. Indeed, with the following php script, that is what we will get.&lt;br /&gt;&lt;pre style="border: 1px solid grey; margin: 10px; padding: 10px; background-color: rgb(255, 238, 238);"&gt;&amp;lt;?php&lt;br /&gt;  $a = "abc";&lt;br /&gt;  $b = $a;&lt;br /&gt;  printf("a is '%s', b is '%s'\n", $a, $b);&lt;br /&gt;?&amp;gt;&lt;br /&gt;&lt;/pre&gt;produces:&lt;br /&gt;&lt;pre style="border: 1px solid grey; margin: 10px; padding: 10px; background-color: rgb(238, 255, 238);"&gt;a is 'abc', b is 'abc'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;An alternative interpretation of the assignment statement, which to many people appears bizarre but which is useful in certain circumstances, is that by assigning &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; to &lt;span style="font-weight: bold;"&gt;$b&lt;/span&gt; we might actually be saying 'make variable B &lt;span style="font-weight: bold; font-style: italic;"&gt;refer to&lt;/span&gt; the &lt;span style="font-style: italic; font-weight: bold;"&gt;same thing&lt;/span&gt; that variable A refers to'. Under this assumption, if we change the value in variable A (or B) after the assignment of &lt;span style="font-weight: bold;"&gt;$a = $b&lt;/span&gt;, then the value that the other variable holds will also change (since they refer to the same thing). Let's try that and see which of our interpretations is correct.&lt;br /&gt;&lt;pre style="border: 1px solid grey; margin: 10px; padding: 10px; background-color: rgb(255, 238, 238);"&gt;&amp;lt;?php&lt;br /&gt;  $a = "abc";&lt;br /&gt;  $b = $a;&lt;br /&gt;  printf("a is '%s', b is '%s'\n", $a, $b);&lt;br /&gt;&lt;br /&gt;  $a = "xyz";&lt;br /&gt;  printf("a is now '%s', b is now '%s'\n", $a, $b);&lt;br /&gt;?&amp;gt;&lt;br /&gt;&lt;/pre&gt;produces:&lt;br /&gt;&lt;pre style="border: 1px solid grey; margin: 10px; padding: 10px; background-color: rgb(238, 255, 238);"&gt;a is 'abc', b is 'abc'&lt;br /&gt;a is now 'xyz', b is now 'abc'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, we can see from this that the first interpretation is correct. Variable &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;$b&lt;/span&gt; are separate things - once the value has been assigned from one to the other, they exist as independent entities. Now let's look at the &lt;span style="font-weight: bold;"&gt;&amp;amp;&lt;/span&gt; operator in this context by using it to assign to a third variable, &lt;span style="font-weight: bold;"&gt;$c&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;Taking our second script and extending it a bit, we now have the following:&lt;br /&gt;&lt;pre style="border: 1px solid grey; margin: 10px; padding: 10px; background-color: rgb(255, 238, 238);"&gt;&amp;lt;?php&lt;br /&gt;  $a = "abc";&lt;br /&gt;  $b = $a;&lt;br /&gt;  $c = &amp;amp;$a; // assign a reference to $a into $c&lt;br /&gt;  printf("a is '%s', b is '%s', c is '%s'\n", $a, $b, $c);&lt;br /&gt;&lt;br /&gt;  $a = "xyz";&lt;br /&gt;  printf("a is now '%s', b is now '%s',&lt;br /&gt;                         c is now '%s'\n", $a, $b, $c);&lt;br /&gt;?&amp;gt;&lt;br /&gt;&lt;/pre&gt;When we run this, it produces:&lt;br /&gt;&lt;pre style="border: 1px solid grey; margin: 10px; padding: 10px; background-color: rgb(238, 255, 238);"&gt;a is 'abc', b is 'abc', c is 'abc'&lt;br /&gt;a is now 'xyz', b is now 'abc', c is now 'xyz'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Hmmm. We assigned &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; to &lt;span style="font-weight: bold;"&gt;$c &lt;/span&gt;before we changed the value of &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt;. Why does &lt;span style="font-weight: bold;"&gt;$c&lt;/span&gt; apparently contain the value that we assigned to &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; after the first print statement? Well, the answer is 'Because &lt;span style="font-weight: bold;"&gt;$c&lt;/span&gt; &lt;span style="font-style: italic;"&gt;contains a reference to&lt;/span&gt; &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt;'. Effectively it points towards &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; and says 'Yeah, what he said!'. Effectively &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;$c&lt;/span&gt; are one and the same thing.&lt;br /&gt;&lt;br /&gt;So, that's the score with 'ordinary' variables and it holds true for both php4 and php5. Let's see now what happens with objects.&lt;br /&gt;&lt;br /&gt;&lt;h2&gt;Objects and references&lt;/h2&gt;In this next section, I'm going to be assuming the existence of the following php object. It has a public name attribute and - for convenience - it 'knows' how to convert itself into a string.&lt;br /&gt;&lt;pre style="border: 1px solid grey; margin: 10px; padding: 10px; background-color: rgb(238, 238, 255);"&gt;&amp;lt;?php&lt;br /&gt;&lt;br /&gt;class testClass {&lt;br /&gt;  var $name;&lt;br /&gt;&lt;br /&gt;  function testClass($name) {&lt;br /&gt;      $this-&gt;name = $name;&lt;br /&gt;  }&lt;br /&gt;&lt;br /&gt;  function __toString() {&lt;br /&gt;      return $this-&gt;name;&lt;br /&gt;  }&lt;br /&gt;}&lt;br /&gt;&lt;br /&gt;?&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Let's try our last script but using this object instead of simple strings.&lt;br /&gt;&lt;br /&gt;&lt;pre style="border: 1px solid grey; margin: 10px; padding: 10px; background-color: rgb(255, 238, 238);"&gt;&amp;lt;?php&lt;br /&gt;  $a = new testClass("My object name");&lt;br /&gt;  $b = $a;&lt;br /&gt;  $c = &amp;amp;$a; // assign a reference to $a into $c&lt;br /&gt;  printf("a is '%s', b is '%s', c is '%s'\n", $a, $b, $c);&lt;br /&gt;&lt;br /&gt;  $a-&gt;name = "New Name";&lt;br /&gt;  printf("a is now '%s', b is now '%s',&lt;br /&gt;                         c is now '%s'\n", $a, $b, $c);&lt;br /&gt;?&amp;gt;&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;Now if we run this, we get one of two possible result. Exactly what you get depends on what version of php you run the code through.&lt;br /&gt;&lt;h3&gt;PHP 4&lt;/h3&gt;Running the script under php4, we get the following:&lt;br /&gt;&lt;pre style="border: 1px solid grey; margin: 10px; padding: 10px; background-color: rgb(238, 255, 238);"&gt;a is 'My object name', b is 'My object name', c is 'My object name'&lt;br /&gt;a is 'New Name', b is 'My object name', c is 'New Name'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;h3&gt;PHP 5&lt;/h3&gt;However, under php5 the result is:&lt;br /&gt;&lt;pre style="border: 1px solid grey; margin: 10px; padding: 10px; background-color: rgb(238, 255, 238);"&gt;a is 'My object name', b is 'My object name', c is 'My object name'&lt;br /&gt;a is 'New Name', b is 'New Name', a is 'New Name'&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;So, what's the reason for the difference? What's going on?&lt;br /&gt;&lt;br /&gt;&lt;br /&gt;Comparing the two versions, we see that under php4 assigning an object variable to a different variable results in two separate objects. Each can be modified independently which is why in the second print statement &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; gives a different answer from &lt;span style="font-weight: bold;"&gt;$b&lt;/span&gt;. Assigning using an explicit reference into &lt;span style="font-weight: bold;"&gt;$c&lt;/span&gt; (&lt;span style="font-weight: bold;"&gt;$c = &amp;amp;$a&lt;/span&gt;) copies a reference into the &lt;span style="font-weight: bold;"&gt;$c&lt;/span&gt; variable so &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; and &lt;span style="font-weight: bold;"&gt;$c&lt;/span&gt; can be considered to be the same. Thus simple variables like strings and complex objects behave essentially the same as each other under php4.&lt;br /&gt;&lt;br /&gt;Under php5, however, we see different behaviour. Here, even though we assign from &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; to &lt;span style="font-weight: bold;"&gt;$b&lt;/span&gt; using a simple assignment statement (&lt;span style="font-weight: bold;"&gt;$b = $a&lt;/span&gt;), because &lt;span style="font-weight: bold;"&gt;$a&lt;/span&gt; is an object we get a reference assignment. This is (apparently) to bring php into line with languages like java. Under php5, the &lt;span style="font-weight: bold;"&gt;&amp;amp;&lt;/span&gt; reference assignment continues to behave as in php4. Thus, there appears to be no way to get php4-like behaviour under php5 as far as objects are concerned.&lt;br /&gt;&lt;br /&gt;In fact, if we want to take copies of an object into a different variable such that each is a separate entity under php5, we have to use the &lt;span style="font-weight: bold;"&gt;clone()&lt;/span&gt; function i.e. &lt;span style="font-weight: bold;"&gt;$b = clone($a)&lt;/span&gt;.&lt;br /&gt;&lt;br /&gt;There now, that hardly hurt at all, did it?&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8574664069419810927-5188129361069779322?l=sportsdaft.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sportsdaft.blogspot.com/feeds/5188129361069779322/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8574664069419810927&amp;postID=5188129361069779322' title='1 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8574664069419810927/posts/default/5188129361069779322'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8574664069419810927/posts/default/5188129361069779322'/><link rel='alternate' type='text/html' href='http://sportsdaft.blogspot.com/2008/04/by-reference-vs-by-value-in-php.html' title='By reference vs. By value in php'/><author><name>Andy Law</name><uri>http://www.blogger.com/profile/16193067228538333296</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>1</thr:total></entry><entry><id>tag:blogger.com,1999:blog-8574664069419810927.post-3987620948583567574</id><published>2008-04-01T12:46:00.004+01:00</published><updated>2008-04-01T13:12:31.338+01:00</updated><title type='text'>Getting SSHKeychain to work on Leopard (OS X 10.5)</title><content type='html'>For several years I've used the excellent utility &lt;a href="http://sshkeychain.org/"&gt;SSHkeychain&lt;/a&gt; to manage my interactions with external servers. I spend a lot of time running multiple shells and moving files from one system to another and the efficiency that a good key agent brings is something that I've come to rely on.&lt;br /&gt;&lt;br /&gt;However, recently I switched to a new machine which came with 10.5 installed and I discovered that Apple have elected to build their own ssh-agent interface into the system.&lt;br /&gt;&lt;br /&gt;&lt;span style="font-weight: bold;"&gt;But it's rubbish.&lt;/span&gt;&lt;br /&gt;&lt;br /&gt;It doesn't remember the keys other than by adding them to the keychain (which I refuse to do) and it can't be configured to drop them on system events like the screen saver kicking in. And if I install SSHKeychain then the system just ignores it. Damn, that's annoying.&lt;br /&gt;&lt;br /&gt;So I googled about a bit and found a couple of posts that talked about the issue, one of which said that &lt;a href="http://projectdaenney.org/2007/sshkeychain-doomsday"&gt;SSHKeychain was dead&lt;/a&gt; and another that came up with a &lt;a href="http://www.dribin.org/dave/blog/archives/2007/11/28/securing_ssh_agent/"&gt;partial solution&lt;/a&gt; that involved extra keychains and an application hack to remove keys from the agent when certain things happen. But no-where - not even on the SSHKeychain site - did I find a recipe to restore SSHKeychain functionality properly.&lt;br /&gt;&lt;br /&gt;So, knowing a bit about UNIX, the way that Apple has implemented its on-demand services daemon and the way that ssh-agents work in general, I've come up with a simple way to get back the control over how my system operates.&lt;br /&gt;&lt;br /&gt;It turns out to be remarkably simple.&lt;br /&gt;&lt;br /&gt;SSH agents work by setting themselves up and listening on a local socket. They advertise this fact by setting an environment variable called SSH_AUTH_SOCK. Programs that need access to the agent look for this environment variable and, if it is set, make a call to it in order to get the necessary authentication.&lt;br /&gt;&lt;br /&gt;SSHKeychain does just this, setting up a socket for each machine user named according to the user's unique (by machine) user id number.&lt;br /&gt;&lt;br /&gt;Unfortunately, Apple uses the same thing to intercept agent requests and overrides the SSHKeychain setting, pointing the SSH_AUTH_SOCK at its own local socket. Requests to the Apple-supplied socket launch the OS built-in agent instead.&lt;br /&gt;&lt;br /&gt;However, there is a way that we can get control of the SSH_AUTH_SOCK variable. We can put an entry in our .profile script (or the [t]csh equivalent .cshrc) to look for the SSHKeychain socket and point at it if it exists.&lt;br /&gt;&lt;br /&gt;So, if you love SSHKeychain as much as I do then run, don't walk, to your favourite text editor and edit (or create) a file called .profile in your login directory to contain the following code:&lt;br /&gt;&lt;br /&gt;&lt;pre&gt;&lt;br /&gt;if [ -e "/tmp/${UID}/SSHKeychain.socket" ];&lt;br /&gt;then&lt;br /&gt;        export SSH_AUTH_SOCK="/tmp/${UID}/SSHKeychain.socket"&lt;br /&gt;fi&lt;br /&gt;&lt;/pre&gt;&lt;br /&gt;&lt;br /&gt;Now, whenever you fire up a terminal, if you have SSHKeychain running then it will be invoked to handle your key requests. The half-a**ed Apple attempt at a key agent GUI will only be used if you've forgotten to run SSHKeychain.&lt;br /&gt;&lt;br /&gt;I hope that helps folks, and I look forward to any comments and/or improvements.&lt;div class="blogger-post-footer"&gt;&lt;img width='1' height='1' src='https://blogger.googleusercontent.com/tracker/8574664069419810927-3987620948583567574?l=sportsdaft.blogspot.com' alt='' /&gt;&lt;/div&gt;</content><link rel='replies' type='application/atom+xml' href='http://sportsdaft.blogspot.com/feeds/3987620948583567574/comments/default' title='Post Comments'/><link rel='replies' type='text/html' href='http://www.blogger.com/comment.g?blogID=8574664069419810927&amp;postID=3987620948583567574' title='7 Comments'/><link rel='edit' type='application/atom+xml' href='http://www.blogger.com/feeds/8574664069419810927/posts/default/3987620948583567574'/><link rel='self' type='application/atom+xml' href='http://www.blogger.com/feeds/8574664069419810927/posts/default/3987620948583567574'/><link rel='alternate' type='text/html' href='http://sportsdaft.blogspot.com/2008/04/getting-sshkeychain-to-work-on-leopard.html' title='Getting SSHKeychain to work on Leopard (OS X 10.5)'/><author><name>Andy Law</name><uri>http://www.blogger.com/profile/16193067228538333296</uri><email>noreply@blogger.com</email><gd:image rel='http://schemas.google.com/g/2005#thumbnail' width='16' height='16' src='http://img2.blogblog.com/img/b16-rounded.gif'/></author><thr:total>7</thr:total></entry></feed>
