2014-09-13 80 views
-2

我有简单的类:分配对象属性

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

'[“pet_owner”] = int(12)'。它**是**集,有什么不对? – Sugar 2014-09-13 11:03:18

+1

'pet_owner'已被设置为'12',完全像它应该是 – 2014-09-13 11:03:34

+1

为什么你要实例化Pet两次?并在每种情况下将对象分配给相同的变量? – 2014-09-13 11:04:19

回答

0

$owner = '12';,而不是$owner = 12;

否则会显示属性INT(12)

,还可以使用$pet = new Pet;此一次而已。