分配对象属性

时间:2014-09-13 11:00:58

标签: php

我有简单的课程:

class Pet { 
 var $pet_type;
 var $pet_exist = true;
 var $pet_name;
 var $pet_owner;
 public static  $pet_types= array (1 =>"Nothing to choose...",
                          2 =>"Dog",
                          3 =>"Cat",
                          4 =>"Guinea pig",
                          5 =>"Turtle",
                          6 =>"Fishes",
                          7 =>"Other");

 function setAttr($pet_exist,$pet_type,$pet_name,$pet_owner) {
    $this->pet_exist=$pet_exist;
    $this->pet_name=$pet_name;
    $this->pet_type=$pet_type;
    $this->pet_owner=$pet_owner;
 }

我试试这个

$pet = new Pet;
$owner = 12;
$pet =  new Pet;
$pet->setAttr("12","12","12",$owner);
var_dump($pet);

我看到以下结果:

object(Pet)#2 (4) { ["pet_type"]= string(2) "12" ["pet_exist"]= string(2) "12" ["pet_name"]= string(2) "12" ["pet_owner"]= int(12) }

为什么不设置属性pet_owner

1 个答案:

答案 0 :(得分:0)

如果你想要它像这样的字符串使用,

$owner = '12';代替$owner = 12;

否则会显示属性 int(12)

并且仅使用$pet = new Pet;一次..