Replacement for getDescribeInstancesIterator in the new PHP AWS SDK (^3)? - php

In the previous version of AWS SDK there were many handy functions like getDescribeInstancesIterator which would return an array iterator for results (in this case an array of instances matching the filters).
This function (and similar) have been removed in the new AWS SDK. After some research I can find instead is a getIterator($name, array $args = []) function defined as an AwsClientTrait.
I'm unsure how I can use this new getIterator function to replace the getDescribeInstancesIterator function, i.e. to get an Array iterator for describeInstances function that returns list of matching instances like before (without having to worry about pagination, etc)?
Any code sample would be extremely useful.

Finally figured it out. The new syntax is like this
$ec2Client->getIterator('FunctionName', 'Values')
So getDescribeInstancesIterator now becomes:
$ec2Client->getIterator('DescribeInstances', [...'Filters'])
This syntax is same for all operations including iterating over files in buckets, getting running instances, etc.

Related

Single array element becomes object with Zend_Soap_Client

I'm using Zend_Soap_Client and encountering this issue:
<parent>
<child><name>abc</name></child>
<child><name>def</name></child>
</parent>
If there's more than one child element then Zend return array and I can access like
$result->parent->child[0]->name
but if there's only one child node it returns object like:
$result->parent->child->name
Can you please let me know what's wrong with my approach or how can I overcome it?
My sample code:
$client = new Zend_Soap_Client('url', array('wsdl'=>'url));
$result = $client->getResult();
I'm using zend 1.9. The same issue happens with PHP's native SoapClient
Thanks!
Personally I do not see the need to use Zend_Soap_Client instead of SoapClient because the Zend version does not add anything beneficial, but on the other hand the solution applies to both:
There is an options array parameter in the original SoapClient that accepts plenty of things, and especially this below (ref):
The features option is a bitmask of SOAP_SINGLE_ELEMENT_ARRAYS,...
With this option, all array structures in the soap response are not reduced to one single element if they contain only one, but left as is. You are always accessing an array then, which is easier than switching depending on the content.
Example:
$s = new SoapClient($wsdl, array('features' => SOAP_SINGLE_ELEMENT_ARRAYS));

MongoDB: Different return values on .find() at shell access, and at php

I started to use MongoDB 2.4.4, and I have a very iritating case for query-ing some post, by field in php.
In the mongoshell, the db.posts.find({page_id:345671} (for example) gives me a 293 count of document.
The php equivalent:
$connection = new Mongo('mongodb://localhost:27017');
$db = connection->selectDB('post_db');
$posts = $db->posts->find(array('page_id' => 345671));
Alway return a zero, but, when the a find array is empty, it gives back the entire collection.
Also, ->explain() and .explain() gaves me different params.
What am I do wrong? There's no sharding, no indexes, just some test data, i'm in the begining of the things.
SOLVED, many thanks to Vitaly Muminov!
"i'm not quite sure about that, but you can try setting ini_set('mongo.native_long', 1); or wrapping your numbers into MongoInt64 class"
The solution is wrap the number to MongoInt64!

PHP convert array to resource

For visual representation, for simplicity and of course to feed my curiosity, I'm wondering how to convert a PHP array into a valid PHP resource.
See the below example:
(example image created with dBug component available at http://dbug.ospinto.com/)
I've made 3 examples:
resource: this is the typical representation of a MySQL resource, visualized as a grid
object: a handmade create object from an array
array: a handmade multidimensional array
As you can see, the resource is a visual beauty, while the object and array are constructed by using multidimensional arrays, using poor numeric array indexes to bind them together :(
What I'm looking for, would probably be something like this:
$resource_var = (resource) $array_var;
What I'm looking for, would probably something like this:
$resource_var = (resource) $array(var)
You will never find that. A resource is an internal data-type in PHP. If (and only if) you write yourself a PHP extension and load it, you could do the following:
$resource = array_resource_create($array);
Your PHP extension then would create that resource (as the mysql extension for example creates its specific resource type) within that array_resource_create function. However, it would be useless, because there is no other function so far that could deal with that resource.
You can't create resource. but you can use native one.
Try with curl for example.
function makeResourceFromArray($array) {
$resource = curl_init();
curl_setopt($resource, CURLOPT_PRIVATE, serialize($array));
return $resource;
}
function makeArrayFromResource($resource) {
return unserialize(curl_getinfo($resource, CURLINFO_PRIVATE));
}
$resource = makeResourceFromArray(['name' => 'test']);
$array = makeArrayFromResource($resource);
The output you show there is nothing to do with it being a resource as such, but the pretty-print function you're using noticing that the variable you've given it points at a database result set, and fetching and displaying the results.
What PHP means by a resource is that the variable doesn't actually hold data within PHP, but is a pointer or reference usable by some lower-level module of code - in this case, a DB library which can use that reference to retrieve the results of the executed query.
If you just want the pretty-print to look similar for an array with a DB-resultset-like structure, then you should simply modify the pretty-print function to do so - you don't need to do anything to the array itself.
A resource is a special type. And a resource is specific to a source that's external. Therefore going backwards wouldn't be possible.
Theoretically, an interface with an instance of the resource would help manage the type - but this is just nonsense theoretical talk that is impossible in PHP.

Why would one want to pass primitive-type parameters by reference in PHP?

One thing that's always bugged me (and everyone else, ever) about PHP is its inconsistency in function naming and parameters. Another more recent annoyance is its tendency to ask for function parameters by reference rather than by value.
I did a quick browse through the PHP manual, and found the function sort() as an example. If I was implementing that function I'd take an array by value, sort it into a new array, and return the new value. In PHP, sort() returns a boolean, and modifies the existing array.
How I'd like to call sort():
$array = array('c','a','b');
$sorted_array = sort($array);
How PHP wants me to call sort():
$array = array('c','a','b');
sort($array);
$sorted_array = $array;
And additionally, the following throws a fatal error: Fatal error: Only variables can be passed by reference
sort(array('c','a','b');
I'd imagine that part of this could be a legacy of PHP's old days, but there must have been a reason things were done this way. I can see the value in passing an object by reference ID like PHP 5+ does (which I guess is sort of in between pass by reference and pass by value), but not in the case of strings, arrays, integers and such.
I'm not an expert in the field of Computer Science, so as you can probably gather I'm trying to grasp some of these concepts still, and I'm curious as to whether there's a reason things are set up this way, or whether it's just a leftover.
The main reason is that PHP was developed by C programmers, and this is very much a C-programming paradigm. In C, it makes sense to pass a pointer to a data structure you want changed. In PHP, not so much (Among other things, because references are not the same as a pointer).
I believe this is done for speed-reason.
Most of the time you need the array you are working on to be sorted, not a copy.
If sort should have returned a new copy of the array then for each time you call sort(); the PHP engine should have copied the array into new one (lowering speed and increasing space cost) and you would have no way to control this behaviour.
If you need the original array to be not sorted (and this doesn't happen so often) then just do:
$copy = $yourArray;
sort($yourArray);

Does foreach always create a copy on a none reference in PHP?

I'm wondering if PHP has this optimization built in. Normally when you call foreach without using a reference it copies the passed array and operates on it. What happens if the reference count to that array is only 1?
Say for example if getData returns some array of data.
foreach(getData() as $data)
echo $data;
Since the array returned by getData() only has one reference shouldn't it just be used by reference and not copied first or does php not have this optimization?
This seems like a simple optimization that could help a lot of badly written code.
I can't say for certain, but PHP normally uses "copy on write", so everything is a reference until you try to write to it, at which time a copy is made and you write to the copy.

Categories