How to get rid of array/object casting in PHP? - php

Some methods/functions only accept/return arrays, others only get/give objects. In my day to day PHP programming I have to repetitively convert objects to array and vice versa. ((object) ['x'=>3] and (array) (new stdClsss())).
Most classes do not implement ArrayAccess. Smartly in JavaScript, these two syntaxes are interchangeable. Is there any hack, workaround to stick to one of them and get rid of variable casting and "Cannot use object of type xxx as array in " or "Cannot use object of type stdClass as arhray in "Cannot access property on non-object" messages.

I came up with ary.
$var= ary(['x' => 'foo', 'y' => 'bar']);
$foo = $var->x; //or
$foo = $var['x'];

Try using php serializing function, instead of trying to convert them.
$array = array();
$obj = serialize($array);
$again = unserialize($obj);
This will help you if your interchange is about to save datum.
if, working with the object is your bulk task, try to define a class library for your object and then, implement your function like ToArray(), ToString(), ToObject() or ToStorable(), instead of converting them in your code with natural functions.

Related

Merge two PHP tags

I want to convert two different PHP tags into a single PHP tag.This may sound a little weird but recently I found a question like this and answer with correct marked.
I can't find I'm currently lost address.
My question:
for example;
$gates = array('t1','t2','t3',t4','t5');
$openGates-> and $gates merge.
Result:
$openGates->t1; or t2,t3.
If I remember correctly, the answer to the problem I found before was;
$openGates->{$gates}; like this. I not sure
How can I do that?
You seem to be confusing objects with arrays. Arrays simply contain data, unless you create them with keys also. Such as:
$gates = array('t1'=>true,'t2'=>false,'t3'=>"maybe",'t4'=>0,'t5'=>50);
Matthew Page is correct in that you should look up PHP OOP for a solution.
That being said, you can cast your array to an object providing it has both keys and values:
$gates = (object) array('t1'=>true,'t2'=>false,'t3'=>"maybe",'t4'=>0,'t5'=>50);
or
$openGates = (object) $gates;
This will allow you to access 'properties' of the object in the way you have demonstrated:
echo $openGates->t1;, for example. The -> operator only applies to objects, which are instances of classes, not arrays.
If you do cast your array to the type of object, be sure that you have both keys and values.
It's not simple for newbie programmer...
At first:
$gates = array('t1','t2','t3','t4','t5');
It's Array
$openGates->
This is class instance. Btw. You can retrieve class instance var like $className->varName
You can't simple merge array and class instance. But you can create new class instance variables by loop.
foreach($gates as $gateKey=>$gateVal) {
$openGates->$gatesVal = NULL;
}
But I think it's, in result should be like that:
$gates = array('t1'=>'opened','t2'=>'closed','t3'=>'closed','t4'=>'opened','t5'=>'opened');
foreach($gates as $gateKey=>$gateVal) {
$openGates->$gateKey = $gateVal;
}
echo $openGates->t1;
// or
foreach($gates as $key=>$val) {
echo $openGates->$key.PHP_EOL;
}
Btw you can simple set $openGates->gates = $gates; and call it like echo $openGates->gates['t1'];

Deleting all objects in the document array MongoDB PHP [duplicate]

To create an empty JSON object I do usually use:
json_encode((object) null);
casting null to an object works, but is there any other preferable way and/or any problem with this solution?
Recommended method
json_decode ("{}") will return a stdClass per default, using the below should therefor be considered safe/portable and correct.
json_encode (new stdClass);
Your solution could work..
The documentation specifies that (object) null will result in an empty object, some might therefor say that your code is valid and that it's the method to use.
PHP: Objects - Manual
If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty.
.. but, try to keep it safe!
Though you never know when/if the above will change, so if you'd like to be 100% certain that you will always will end up with a {} in your encoded data you could use a hack such as:
$empty = json_decode ("{}");
$result = json_encode($empty); // "{}"
Even though it's tedious and ugly I do assume/hope that json_encode/json_decode is compatible with one another and always will evaluate the following to true:
$a = <something>;
$a === json_decode (json_encode ($a));
If you use objects as dynamic dictionaries (and I guess you do), then I think you want to use an ArrayObject.
It maps into JSON dictionary even when it's empty. It is great if you need to distinguish between lists (arrays) and dictionaries (associative arrays):
$complex = array('list' => array(), 'dict' => new ArrayObject());
print json_encode($complex); // -> {"list":[],"dict":{}}
You can also manipulate it seamlessly (as you would do with an associative array), and it will keep rendering properly into a dictionary:
$complex['dict']['a'] = 123;
print json_encode($complex); // -> {"list":[],"dict":{"a":123}}
unset($complex['dict']['a']);
print json_encode($complex); // -> {"list":[],"dict":{}}
If you need this to be 100% compatible both ways, you can also wrap json_decode so that it returns ArrayObjects instead of stdClass objects (you'll need to walk the result tree and recursively replace all the objects, which is a fairly easy task).
Gotchas. Only one I've found so far: is_array(new ArrayObject()) evaluates to false. You need to find and replace is_array occurrences with is_iterable.
Well, json_encode() simply returns a string from a PHP array/object/etc. You can achieve the same effect much more efficiently by doing:
$json = '{}';
There's really no point in using a function to accomplish this.
UPDATE
As per your comment updates, you could try:
$test = json_encode(array('some_properties'=>new stdClass));
Though I'm not sure that's any better than what you've been doing.
json_encode($array, JSON_FORCE_OBJECT) will do it too. see https://www.php.net/manual/en/function.json-encode.php

Get objects instead of an array with CodeIgniter + MongoDB

While using Codeigniter and its built-in class Mongo, is it possible to get objects instead of arrays?
I'd rather access documents via $doc->id than $doc['id']. Thanks.
The PHP MongoDB lib will always return result as array. If you wish to work with objects instead of arrays you will need to handle conversion yourself. As converting to an object is not as easy as casting type you would have to write your own function to convert it to an object.
You could do something in a sense
$obj = new stdClass();
foreach($mongoResult as $key => $val){
$obj -> $key = $val;
}
Obviously this will work on basic result sets. You would need to write a more sophisticated function to handle more complex arrays.
I believe the issue has to do with how you are querying the database.
$this->db->query()->get->result();
Will give you objects.
$this->db->query()->get()->result_array();
Will give you arrays

Best way to create an empty object in JSON with PHP?

To create an empty JSON object I do usually use:
json_encode((object) null);
casting null to an object works, but is there any other preferable way and/or any problem with this solution?
Recommended method
json_decode ("{}") will return a stdClass per default, using the below should therefor be considered safe/portable and correct.
json_encode (new stdClass);
Your solution could work..
The documentation specifies that (object) null will result in an empty object, some might therefor say that your code is valid and that it's the method to use.
PHP: Objects - Manual
If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created. If the value was NULL, the new instance will be empty.
.. but, try to keep it safe!
Though you never know when/if the above will change, so if you'd like to be 100% certain that you will always will end up with a {} in your encoded data you could use a hack such as:
$empty = json_decode ("{}");
$result = json_encode($empty); // "{}"
Even though it's tedious and ugly I do assume/hope that json_encode/json_decode is compatible with one another and always will evaluate the following to true:
$a = <something>;
$a === json_decode (json_encode ($a));
If you use objects as dynamic dictionaries (and I guess you do), then I think you want to use an ArrayObject.
It maps into JSON dictionary even when it's empty. It is great if you need to distinguish between lists (arrays) and dictionaries (associative arrays):
$complex = array('list' => array(), 'dict' => new ArrayObject());
print json_encode($complex); // -> {"list":[],"dict":{}}
You can also manipulate it seamlessly (as you would do with an associative array), and it will keep rendering properly into a dictionary:
$complex['dict']['a'] = 123;
print json_encode($complex); // -> {"list":[],"dict":{"a":123}}
unset($complex['dict']['a']);
print json_encode($complex); // -> {"list":[],"dict":{}}
If you need this to be 100% compatible both ways, you can also wrap json_decode so that it returns ArrayObjects instead of stdClass objects (you'll need to walk the result tree and recursively replace all the objects, which is a fairly easy task).
Gotchas. Only one I've found so far: is_array(new ArrayObject()) evaluates to false. You need to find and replace is_array occurrences with is_iterable.
Well, json_encode() simply returns a string from a PHP array/object/etc. You can achieve the same effect much more efficiently by doing:
$json = '{}';
There's really no point in using a function to accomplish this.
UPDATE
As per your comment updates, you could try:
$test = json_encode(array('some_properties'=>new stdClass));
Though I'm not sure that's any better than what you've been doing.
json_encode($array, JSON_FORCE_OBJECT) will do it too. see https://www.php.net/manual/en/function.json-encode.php

php array selector unsure of differnce

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)

Categories