If we use mysql_fetch_object, it returns recordset as an object.
I want to know that is this object is object of some particular class?
Or simply, returned object by mysql_fetch_object is object of which class?
The documentation itself states, that the resulting class will be stdClass or whatever you pass as the second parameter to the function.
Related
In codeigniter 4 there is a Db helper or query builder when i print_r($db->getLastQuery()) it returns whole methods and other information or when echo $db->getLastQuery() it returns last query.
How the same function returns diffrent output at same time ??
Probably because getLastQuery returns object which implements magic method __toString, and with echo variable is casted to string, while print_r just prints raw object.
Let's assume we have this function
<?php
function run($callback)
{
$callback($some, $params);
}
How to call it?
run($someObject->callback);
or rather
run([$someObject, 'callback']);
The first one seems better to me especially because of code suggestion but in documentation is used array notation.
Why is first one worse than second one?
The array notation is better because the arrow notation doesn't work. Functions in PHP aren't first class objects which can be passed around. Whenever you "pass a function", you must pass it by name; i.e. only its name. To pass an object method, you need to pass the object and the method name using the callable pseudo type notation.
I have a PHP class (POJO_FOO) which maps to a table (TABLE_FOO).
e.g. one row equal to one object of that class.
Now I am writing a manager which returns array of such objects matching a particular query. Using PDO, how can I return array of objects ?
When I do simple fetchAll, it returns array (representing number of results) of associative array (column => value). Is there a option in fetchALL which can give me result in form of array of objects ?
you can use PDO::FETCH_CLASS to hydrate your class with your data :
return $pdo->query('SELECT * FROM tablefoo')->fetchAll(PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE,'POJO_FOO');
it is also useful to use PDO::FETCH_CLASS|PDO::FETCH_PROPS_LATE because it makes the construction of the object more consistent. Habitualy your constructor is called before everything. If you do not use FETCH_PROPS_LATE it will called after your properties are hydrated.
hi I working with Drupal and it uses arrays to a level I'm not very familier with, I've a quick question which is what is the difference between these 'selectors'(is that the right term)?
This causes an error "Fatal error: Cannot use object of type stdClass as array in..."
$node['field_geoloc']
this works (im using it in an if != null statement)
$node->field_geoloc
hopefully an easy question...
thanks.
Pretty easy.. the error says it all:
"Fatal error: Cannot use object of type stdClass as array in..."
You are attempting to use an object as an array.
Object properties aren't accessibly using the $array['key'] method that you are used to. You need to access properties like:
`$object->property`
If you have an object, you can get the properties from that array by using the get_object_vars method. But I know from experience that you should not use that method with a $node in Drupal.
-> is operator for accessing public object properties (and call public methods). In order for an object properties to be accessed with $object['key'] syntax, it have to implement ArrayAccess. Other option is to cast the object to array ( $node = (array) $node (but this will work only for first-level keys, e.g. it will turn $node->page to $node['page'], but not $node->page->title to $node['page']['title'] - the later will be accessible via $node['page']->title
Because you can't use object as an array.
That first is an array and that second is an object.
The first one is an array, the second one is an object (of class StdClass).
But you may be interested in this interface: http://php.net/manual/en/class.arrayaccess.php which allows accessing an object as an array (so you do $obj['key'] instead of $obj->key)
I'm working with an object fed back from a class. For some reason, the class spits out an object with numbered properties (0, 1, 2, etc.). I need to check if the object is empty. The standard trick, empty(get_object_vars($obj)), won't work, because get_object_vars returns an empty array even when the object has (numbered) properties.
For reference, the object I'm working with is the one returned by the legislatorsZipCode method of the PHP interface for Sunlight's API. You can see a print_r of a sample response here.
Judging by the code, the author made the mistake of casting a numerically indexed array to an object. This makes getting object properties by name impossible, though you should still be able to foreach over it. You can also simply cast it back to an array: $results = (array) $obj;. Then, count the array.
This appears to work:
if (current($obj) === false)
echo "is empty\n";
It probably is doing an implicit cast to an array.