php
  • oop
  • xpath
  • simplexml
  • 2017-01-20 9 views 1 likes 
    1
    class ad_xml_model extends \ITico_core\common_class { 
    
        public function get_info_from_ad($input_id) { 
         $xml_file = parent::get_set('xml'); 
         $ad_info = $xml_file->xpath("//ad[@id='" . $input_id . "']"); 
         parent::get_set('ad_info', $ad_info); 
         return $ad_info; 
        } 
    
    } 
    

    上記のコードはユニットテスト時に機能しますが、文字列とintの両方を関数に渡してみました。 渡されたパラメータはxpathで動作しませんが、ユニットテストでは動作しますが

    <h2>get_info_from_ad</h2> 
    test data: 1<br> 
    info from ad:- <br> 
    <?php 
    $ad_xml_model->get_info_from_ad(1); 
    $ad_info = $ad_xml_model->get_set('ad_info'); 
    print_r($ad_info); 
    

    が、コントローラから呼び出されたときに以下のコードは

    class main_controller(){ 
        $ad_top_limit = count($ads_from_category); 
        $key = rand(0, $ad_top_limit - 1); 
        $chosen_ad = $ads_from_category[$key]; 
        parent::get_set('chosen_ad', $chosen_ad); 
        $ad_info = $ad_xml_model->get_info_from_ad($chosen_ad); 
        parent::get_set('ad_info', $ad_info); 
        if ($ad_info != null) { 
         switch ($ad_type) { 
          case NUll: 
           break; 
          case 'long': 
           $long_view = new long_view($ad_info); 
           $long_view->show_ad(); 
         } 
        } 
    } 
    

    と私はすべて行ってきた

    <?php 
    echo $main_controller->get_set('chosen_ad') . "<br>"; 
    ?> 
    ad information:-: <br> 
    <?php 
    print_r($main_controller->get_set('ad_info')); 
    

    screenshot of the debug page

    デバッグページのテストを働いていないので

    すべての変数をエコーし​​て、それらがnullではないことを確認するステップのステップ何らかの理由でxpathはメインコントローラから呼び出されたときには機能しませんが、全く同じパラメータが渡されているにもかかわらずユニットテストからうまく機能します。

    答えて

    0

    私は解決策は、暗黙のうちに、私はそれはまだ他の誰かがこれに光を共有することができ、おそらく、動作しない文字列に設定した場合、それはいくつかの理由でint型

    public function get_info_from_ad($input_id) { 
        $xml_file = parent::get_set('xml'); 
        $ad_info = $xml_file->xpath("//ad[@id='" . (int)$input_id . "']"); 
        parent::get_set('ad_info', $ad_info); 
        return $ad_info; 
    } 
    

    たXPathを伝えることがわかりました。

    関連する問題