excecute() method always return an "informational" object after use current() method i get an array开发者_运维问答 always.
How can i obtain an object (with objects) to iterate like this, (no ORM):
foreach($obj as $o)
{
echo $o->name;
echo $o->email;
}
Instead of using current, next, etc.
PHP 5 provides a way for objects to be defined so it is possible to iterate through a list of items, with, for example a foreach statement. By default, all visible properties will be used for the iteration.
Source: http://www.php.net/manual/en/language.oop5.iterations.php
class MyClass
{
public $var1 = 'value 1';
public $var2 = 'value 2';
public $var3 = 'value 3';
protected $protected = 'protected var';
private $private = 'private var';
function iterateVisible() {
echo "MyClass::iterateVisible:\n";
foreach($this as $key => $value) {
print "$key => $value\n";
}
}
}
$class = new MyClass();
foreach($class as $key => $value) {
print "$key => $value\n";
}
echo "\n";
$class->iterateVisible();
Result:
var1 => value 1
var2 => value 2
var3 => value 3
MyClass::iterateVisible:
var1 => value 1
var2 => value 2
var3 => value 3
protected => protected var
private => private var
I really like the standalone Doctrine ArrayCollection class:
Code: http://trac.doctrine-project.org/browser/trunk/lib/Doctrine/Common/Collections/ArrayCollection.php?rev=7481
API: http://www.doctrine-project.org/api/common/2.0/doctrine/common/collections/arraycollection.html
$objects = new Doctrine\Common\Collections\ArrayCollection(); $objects[] = new MyClass('name 1', 'email 1'); $objects[] = new MyClass('name 1', 'email 1'); foreach ($objects as $o) { echo $o->name; echo $o->email; }
Execute()
returns an array, and using current()
you get the result set as an array of arrays, reading kohana V3 API, I found this:
$users = DB::select()->from('users')->where('salesman', '=', TRUE)
->as_object()->execute();
foreach ($users as $user)
{
echo $user->email;
}
精彩评论