What Is #<Some-Number> Next to Object(Someclass) in Var_Dump of an Object? I Have an Inference. am I Right

What is # some-number next to object(someClass) in var_dump of an object? I have an inference. Am I right?

That number is Z_OBJ_HANDLE_PP(struc) where struc is a zval which leads to Z_OBJVAL(zval).handle which leads to (zval).value.obj.

See as well http://php.net/manual/en/internals2.variables.intro.php

In short I would say it's the object identifier written in decimal form (ref):

php_printf("%sobject(%s)#%d (%d) {\n", COMMON, class_name, Z_OBJ_HANDLE_PP(struc), myht ? zend_hash_num_elements(myht) : 0);

And not the count of objects ever created.

how to get key from object(stdClass)[key]?

  1. If you use XDebug you get this in the brackets, if you don't it will be after the hash like this object(stdClass)#1
  2. This is object identifier, which you should not rely or care in real life. See: http://php.net/manual/en/language.oop5.references.php
  3. You can get the object ID using spl_object_id, but that seems like a bad idea. Consider if the object gets destroyed that ID might get reused and your will not have any real meaning.

You can also take a look at a similar SO question What is # next to object(someClass) in var_dump of an object? I have an inference. Am I right?

Issue with the output objects

  1. As explained here sign # is PHP internal identifier for object.
  2. Same identifiers for different objects means that your first object (tx_job_dagoupost) was transient (not assigned to any variable or variable was unset) and PHP used same identifier for next object.

spl_object_hash for PHP 5.2 (unique ID for object instances)

I ran a couple of quick tests. I really think you'd be better off storing real callbacks in your bind() function using bind('evt_name', array($obj, 'callback_function')). If you absolutely want to go the spl_object_hash route, rather than storing references with the event bindings, you're looking at something like this:

A var_dump / extract and hash id implementation:

function spl_object_hash_var_dump($object){
if (is_object($object)){
ob_start();
var_dump($object);
$dump = ob_get_contents();
ob_end_clean();
if (preg_match('/^object\(([a-z0-9_]+)\)\#(\d)+/i', $dump, $match)) {
return md5($match[1] . $match[2]);
}
}
return null;
}

A naive references implementation:

function spl_object_dumb_references(&$object) {
static $hashes;

if (!isset($hashes)) $hashes = array();

// find existing instance
foreach ($hashes as $hash => $o) {
if ($object === $o) return $hash;
}

$hash = md5(uniqid());
while (array_key_exists($hash, $hashes)) {
$hash = md5(uniqid());
}

$hashes[$hash] = $object;
return $hash;
}

This one was basically 5-50x worse than the class-based reference function across the board, so it's not worth worrying about.

A store references by class implementation:

function spl_object_hash_references(&$object) {
static $hashes;

if (!isset($hashes)) $hashes = array();

$class_name = get_class($object);
if (!array_key_exists($class_name, $hashes)) {
$hashes[$class_name] = array();
}

// find existing instance
foreach ($hashes[$class_name] as $hash => $o) {
if ($object === $o) return $hash;
}

$hash = md5(uniqid($class_name));
while (array_key_exists($hash, $hashes[$class_name])) {
$hash = md5(uniqid($class_name));
}

$hashes[$class_name][$hash] = $object;
return $hash;
}

And you end up with results that look like this. Summary: the class based references implementation performs best around n/50 classes--at its best, it manages to pull off 1/3 the performance of the var_dump based implementation, and it's usually much worse.

The var_dump implementation seems to be tolerable, though not ideal. But if you're not doing too many of these lookups, it won't be a bottleneck for you. Especially as a fallback for PHP < 5.2 boxen.

Class doesn't fetch correctly the object's values

You shouldn't use $ sign to access object properties. This is correct:

 $this->id = $id;


Related Topics



Leave a reply



Submit