【问题标题】:Class could not be converted to string类无法转换为字符串
【发布时间】:2016-03-11 04:08:59
【问题描述】:

我正在尝试为一家轮胎公司的客户列表构建代码。我已经完成了所有工作,但我仍然需要一个函数来更新单个客户并回显一个字符串,其中包含 .txt 文件中每个帖子的特定 ID。这是我所有的代码,因为当我尝试从数组中写出每个 id 时,我得到了这个:

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

这是我使用的代码 $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”按钮一样,即“删除”在瑞典语中。

【问题讨论】:

  • 你真的是想回显$obj吗?它似乎是一个对象。
  • 因为我像这样分配数组中的每个帖子,是的。 foreach($company->getCustomerList() as $key => $obj)

标签: php arrays oop


【解决方案1】:

由于这个问题没有得到解答,而且您显然已经付出了很多努力,您的问题在于这一行:

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 "&lt;br/&gt;" . $key."$obj .&lt;br&gt;"

(看起来像是错字)

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

您应该删除代码那部分中的$obj在您的Customer 类中提供__toString() 方法,以便echo 知道如何转换Customer反对一个字符串。

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多