D language: how do you do such as Php array(); foreach()?
How can i do t开发者_高级运维his $array and $object and foreach() interpretation from Php to D correctly?
Php (pro):
class Zend_Models
{
public static function getSome()
{
$array = array(
"a" => "b",
"b" => "b"
);
$object = (object) $array;
foreach($object as $value)
{
$this->view->inject[] = $value;
}
// Zend_Debug::dump($this->view->inject);
return "ok";
}
public static getAbove()
{
return self::getSome();
}
}
D (incubator, doing mistakes):
import std.stdio;
class Zend_Models
{
void static getSome()
{
//?...
}
}
I think this would do it:
import std.stdio;
class Zend_Models
{
string getSome()
{
auto array = ["a", "b"];
foreach(value; array)
{
this.view.inject ~= value;
}
return "ok";
}
}
string getAbove() {
return getSome();
}
}
That said, you probably shouldn't try to write PHP in D. It's probably better to use exceptions than to return a status code, and if you do return a status code, an enum is probably better than a string.
精彩评论