dsubq24666
2013-10-21 04:24
浏览 39
已采纳

像在java中一样将php对象传递给数组/列表

maybe its duplicate but i cant find it.

    something = new ArrayList<>(); 
something.add(new Object("Hello"));
something.add(new object("World"));
something.add(new Object("!"));
for(blablabla){
System.out.print(something.get(i).getTextFromConstructor());
}

this will print "Hello World!"

in php i dont know solution to pass whole objects into array to call their methods from loop or just by something[0]->method(); As i know this in php cannnot be done, but maybe i am wrong :-)

Thank You

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • duanchao4445 2013-10-21 04:27
    已采纳
    $arr = array();
    $arr[] = new MyUSerDefinedObject("Hello");
    //...
    
    echo $arr[0]->methd();
    //or
    foreach ($arr as $val) {
        echo $val->methd();
    }
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dongxiaoke2018 2013-10-21 04:28

    Use this example to set the bridge b/w php and java so that you can pass values to them

    <?php
    $date = new Java("java.util.Date", 70, 9, 4);
    var_dump($date->toString());
    $map = new Java("java.util.HashMap");
    $map->put("title", "Java Bridge!");
    $map->put("when", $date);
    echo $map->get("when")->toString()."
    ";
    echo $map->get("title")."
    ";
    
    $array = array(1,2,3,4,5);
    $map->put("stuff", $array);
    var_dump($map->get("stuff"))."
    ";
    
    $system = new JavaClass("java.lang.System");
    echo "OS: ".$system->getProperty("os.name")."
    ";
    $math = new JavaClass("java.lang.Math");
    echo "PI: ".$math->PI."
    ";
    ?>
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题