the PHP mysqli_result class implements the Traversable interface for some time now. This gives some interesting possibilities like the following piece of code:
<?php
$db = new mysqli( '...' );
$result = $db->query( '...' );
foreach( $result as $row ) {
//$row is an associative array containing the fetched values
}
Now, I like this pretty much, but in the library I am currently working on I would like to have objects fetched, not associative arrays. I also want to be able to pass a class to it, so I can create a simple model system with it
For that I extend it like this:
<?php
class mysqli_result_extension extends mysqli_result {
protected $result_class;
public function set_result_class( $class ) { $this->result_class = $class; }
public function function_to_overwrite() {
return $this->fetch_object( $this->result_class );
}
}
My problem and my question to you:
How do I switch from fetch_assoc to fetch_object now? I would like to overwrite the method the Traversable interface uses in order to return the result, but it doesn't define any (http://php.net/manual/en/class.traversable.php)
So what would I need to overwrite in order to switch from fetch_assoc to fetch_object and to be able to pass my $result_class to it so I am able to do something like this:
<?php
$db = new mysqli_extension( '...' );
$posts = $db->query( 'select * from posts' );
//$result is object of type mysqli_result_extension
$posts->set_result_class( 'post' );
foreach( $posts as $post ) {
//$post is an instance of class 'post'
}
I don't want something dirty, hacky like overwriting fetch_assoc and just returning the object in it
Thanks in advance for your help and input