Skip to content

Instantly share code, notes, and snippets.

@bayleedev
Created September 17, 2012 15:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bayleedev/3737988 to your computer and use it in GitHub Desktop.
Save bayleedev/3737988 to your computer and use it in GitHub Desktop.
Will compact and extract keep the object reference?
<?php
$provider = new stdClass();
echo '<pre>';
echo 'First: ' . spl_object_hash($provider) . PHP_EOL;
function testProvider(stdClass $provider) {
$provider->name = 'Blaine';
return compact($provider);
}
extract(testProvider($provider));
echo 'Name: ' . $provider->name . PHP_EOL;
echo 'First: ' . spl_object_hash($provider);
@bayleedev
Copy link
Author

Possibly overwriting the old object which lets object hash reuse it's id. Testing again and changing it's name.

<?php

function testProvider($items) {
    extract($items);
    $providers = $provider;
    $providers->name = 'Blaine';
    return compact("providers");
}

$provider = new stdClass();
$provider->name = 'Chris';

echo '<pre>';

echo 'Name: ' . $provider->name . PHP_EOL;

echo 'Hash: ' . spl_object_hash($provider) . PHP_EOL;

extract(testProvider(compact("provider")));

echo 'Name: ' . $provider->name . PHP_EOL;

echo 'Hash: ' . spl_object_hash($provider) . PHP_EOL;

echo 'Name: ' . $providers->name . PHP_EOL;

echo 'Hash: ' . spl_object_hash($providers) . PHP_EOL;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment