类无法转换为字符串

时间:2015-12-06 14:41:08

标签: php arrays oop

我正在尝试为一家轮胎公司建立一个客户名单代码。我已经使一切工作,但我仍然需要一个功能,以便能够更新单个客户,并且还回显一个字符串,其中包含.txt文件中每个帖子的特定ID。这是我的所有代码,因为当我尝试从数组中写出每个id时,我得到了这个:

第51行的/Applications/MAMP/htdocs/douglas/classes_serialisering.php中无法将Customer对象转换为字符串

这是我使用$ key(“。$ obj
”)的代码。 ....

以下是该计划的所有代码:

首先:classes_serialisering.php

<?php
//
// Registrera alla klassfiler som behövs.
spl_autoload_register(function ($class) {
include 'classes/' . $class . '.class.php';
});
global $company;
$company = new Company("Ragnars Däckhotell");

//
// Lägg till lagerpost i textfilen
if(isset($_REQUEST["addPart"])){
if(strlen($_REQUEST["customername"])>0){
$company->addCustomer($_REQUEST["customername"],                 $_REQUEST["customertfn"], $_REQUEST["customerepost"],   $_REQUEST["tirebrand"], $_REQUEST["status"]);
//
//Spara information till filen med lagerinformationen.
    file_put_contents("../objdata.txt",serialize($company-      >getCustomerList()));
 }
 unset($_REQUEST["addPart"]);
 header("Location: classes_serialisering.php");
 exit();
 }
 //
 // Radera en post i lagerregistret
 if(isset($_REQUEST["delPart"])){
 $company->removeCustomer($_REQUEST["delPart"]);
 //
 // Spara all ny information till textfilen med lagerinfon.
 file_put_contents("../objdata.txt",serialize($company- >getCustomerList()));
 unset($_REQUEST["delPart"]); // Disable button press
 header("Location: classes_serialisering.php");
 exit();
 }
 ?>
 <!DOCTYPE html> 
 <html>
 <head>
    <title> Webb 2 M3 </title>
    <meta charset=utf-8/>
 <style>
    body {font: Verdana; font-size: 1em;}
 </style>
 </head>
 <body>
 <form action="" method="post">
    <p>
 <?php
 //
 // Skriv ut alla poster i registret kopplade till array id.
 foreach($company->getCustomerList() as $key => $obj){
 echo "<br/>" . $key."$obj .<br>". $obj->getcustomername() . ", " .        $obj->getcustomertfn() . ", " . $obj->getcustomerepost() . ", " . $obj- >gettirebrand() . ", " . $obj->getstatus() . ", " . $obj->getdate() .
 " <a href='classes_serialisering.php?delPart=$key'>Radera " . $obj-     >getcustomername() . " </a>";
 }
 ?>
    </p>
    <p>
        Namn: <input type="text" name="customername" /><br/>
        Telefonnummer: <input type="text" name="customertfn" /> <br/>
        E-post: <input type="text" name="customerepost" /> <br/>
        Märke & Dimension: <input type="text" name="tirebrand" /> <br/>
        Status: <input type="text" name="status" /> <br/>
        <input type="submit" name="addPart" value="Lägg Till Kund"/>
    </p>
 </form>
 </body>
 </html>

这是company.class

<?php
interface RegistrerCustomer {
function addCustomer($customername, $customertfn, $customerepost,       $tirebrand, $status);
function getCustomerList();
}

class Company implements RegistrerCustomer { 
protected $companyname = '';
private $CustomerList = array();

function __construct($companyname){
    $this->foretag = $companyname;

//Hämta textfil med kundlistan.
    if(file_exists("../objdata.txt"))
        $this->CustomerList =        unserialize(file_get_contents("../objdata.txt"));
}
function setCompanyname($companyname){
    $this->Companyname = $companyname;
}
function getCompanyname(){
    return $this->Companyname;  
}
function addCustomer($customername, $customertfn, $customerepost,     $tirebrand, $status){
    $this->CustomerList[] = new Customer($customername, $customertfn,   $customerepost, $tirebrand, $status, Date('c'));
}
function getCustomerList(){
    return $this->CustomerList;
}
function removeCustomer($ind){
    unset($this->CustomerList[$ind]);
}
}

?>

Customer.class.php:

<?php
//
// En klass för all information som skall kunna lagras i textfilen med       lagerinformationen
abstract class Customerpost {
abstract public function __construct($customername, $customertfn,         $customerepost, $tirebrand, $status, $datum);
abstract function setcustomername($customername);
abstract function getcustomername();

abstract function setcustomertfn($customertfn);
abstract function getcustomertfn();
abstract function setcustomerepost($customerepost);
abstract function getcustomerepost();
abstract function settirebrand($tirebrand);
abstract function gettirebrand();
abstract function setstatus($status);
abstract function getstatus();
abstract function setdate($datum);
abstract function getdate();
}
//
// En extension för klassen ovan
 class Customer extends Customerpost { 
protected $customername = '';
protected $customertfn = '';
protected $customerepost = '';
protected $tirebrand = '';
protected $status = '';
protected $date = '';

function __construct($customername, $customertfn, $customerepost,     $tirebrand, $status, $datum){ 
    $this->customername = $customername;
    $this->customertfn = $customertfn;
    $this->customerepost = $customerepost;
    $this->tirebrand = $tirebrand;
    $this->status = $status;
    $this->date = $datum;
}
function setcustomername($customername){
    $this->customername = $customername;
}
function getcustomername(){
    return $this->customername;
}
function setcustomertfn($customertfn){
    $this->customertfn = $customertfn;
}
function getcustomertfn(){
    return $this->customertfn;
}
function setcustomerepost($customerepost){
    $this->customerepost = $customerepost;
}
function getcustomerepost(){
    return $this->customerepost;
}
function setPnr($pnum){
    $this->tfn = $customertfn;
}
function getPnr(){
    return $this->customertfn;
}
function setEpost($email){
    $this->customerepost = $customerepost;
}
function getEpost(){
    return $this->customerepost;
}
function settirebrand($tirebrand){
    $this->tirebrand = $tirebrand;
}
function gettirebrand(){
    return $this->tirebrand;
}
function setstatus($status){
    $this->status = $status;
}
function getstatus(){
    return $this->status;
}
function setdate($datum){
    $this->date = $datum;
}   
function getdate(){
    return $this->date;
}
}
?>

为什么它不能回应每个id的任何想法?而且,我应该如何制作一个功能,以便能够在填写表格后通过每个帖子右侧的按钮更新每个帖子,然后按“更新”,就像我现在拥有的'Radera'按钮,这是'删除'瑞典语。

1 个答案:

答案 0 :(得分:0)

由于这个问题没有得到解决,而且你已经做了很多努力,你的问题在于这一点:

echo "<br/>" . $key."$obj .<br>". $obj->getcustomername() . ", " .$obj->getcustomertfn() . ", " .$obj->getcustomerepost() . ", " . $obj- >gettirebrand() . ", " . $obj->getstatus() . ", " . $obj->getdate() ." <a href='classes_serialisering.php?delPart=$key'>Radera " . $obj->getcustomername() . " </a>";

具体来说,你已经完成了:

echo "<br/>" . $key."$obj .<br>"

(看起来像一个错字)

这意味着您正在回显键和值,但是,该值实际上是Customer对象,echo除非您提供__tostring(),否则不会尝试转换为字符串Customer课程中的方法。 http://php.net/manual/en/language.oop5.magic.php#object.tostring

您应该删除代码中<{1>}部分中的$obj__toString()课程中提供Customer方法,以便echo知道如何将Customer对象转换为字符串。

我还建议为您的方法选择一致的命名约定,通常是camelCasesnake_case,因为这会使您的代码更具可读性。

请分别询问您问题的第二部分,因为这是一个不同的问题,并且您提供了大量代码供您查看,我们都必须尝试解读这些代码。我你问如何unserialize,更新然后重新serialize你的对象,所以这些页面将是一个很好的起点。