drcj64241 2015-02-23 09:10
浏览 35
已采纳

打印对象内部的对象变量

Well, ive been trying to get my crm to print multiple contacts for each company but i cant get it to work

Company is a class,companycontactis a class

//class called company

 function __construct($idklanten,$naam,$adres,$postcode,$stad,$contacten){
    $this->idklanten=$idklanten;
    $this->naam=$naam;
    $this->adres=$adres;
    $this->postcode=$postcode;
    $this->stad=$stad;
    $this->contacten=$contacten;

}

//class called contact

    function __construct($idcontactklanten,$voornaam,$tussenvoegsel,$achternaam,$tel,$email,$klantID){
    $this->idcontactklanten=$idcontactklanten;
    $this->voornaam=$voornaam;
    $this->tussenvoegsel=$tussenvoegsel;
    $this->achternaam=$achternaam;
    $this->tel=$tel;
    $this->email=$email;
    $this->klantID=$klantID;
}

//getname for a contact

  function getNaam() {
    if(strlen($this->gettussenvoegsel()) == 0) {

        return $this->getvoornaam()." ".$this->getachternaam()."";
    }
    else {


        return $this->getvoornaam() . " "  . $this->gettussenvoegsel() . " " .  $this->getachternaam();
    }
}

//function for getting the names from my object company,array with objects of contacts

 function getcontacten(){           
        $ct=$this->contacten[$teller];
        $txt="";
        for($teller=0;$teller<10;$teller++){
            $txt+=$ct->getNaam()."<br>";
        }
     return $txt;
}

then on my index page when i call getcontacten() it does not work comparing to my other get function which do work. it just outputs a 0

Any help is appreciated

  • 写回答

1条回答 默认 最新

  • dqeq885710 2015-02-23 09:37
    关注

    Your biggest error would be the following:

    $txt+=$ct->getNaam()."<br>";
    

    Should be

    $txt.=$ct->getNaam()."<br>";
    

    Because to append to a string you use ".=", not "+=".

    Also I don't know if the other part of you code works, I would write something like the following:

    $txt = "";
    foreach ($this->contacten as $ct){
        $txt .= $ct->getNaam() . "<br />";
    }
    return $txt;
    

    or

    $txt = "";
    for ($i = 0; $i < count($this->contacten); $i++){
        $txt .= $this->contacten[$i]->getNaam() . "<br />";
    }
    return $txt;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度