Php 如何访问数组中包含的对象中的属性-请参见代码

Php 如何访问数组中包含的对象中的属性-请参见代码,php,arrays,oop,Php,Arrays,Oop,如何访问数组中包含的对象中的属性 为什么下面的代码不起作用 <?php class Car{ private $model; private $color; private $price; public function __car($model, $color, $price) { this.$model = $model; this.$color = $color; this.$price = $

如何访问数组中包含的对象中的属性

为什么下面的代码不起作用

<?php

class Car{
    private $model;
    private $color;
    private $price;
    public function __car($model, $color, $price)
    {
        this.$model = $model;
        this.$color = $color;
        this.$price = $price;
    }
}

$cars = [];
$jetta = new Car("Jetta", "Red", 2500);
$cars[] = $jetta;

$cobalt = new Car("Cobalt", "Blue", 3000);
$cars[] = $cobalt;

// this is the part of the code that doesn't work
// I need to output the values from the objects, model, color and price
echo $cars[0]->$model; 
echo $cars[0]->$color;
echo $cars[0]->$price;

您的语法和构造函数错误

以下是最终代码:

<?php

class Car{
    // the variables should be public
    public $model;
    public $color;
    public $price;
    // this is how you write a constructor
    public function __construct($model, $color, $price)
    {
        // this is how you set instance variables
        $this->model = $model;
        $this->color = $color;
        $this->price = $price;
    }
}

$cars = [];
$jetta = new Car("Jetta", "Red", 2500);
$cars[] = $jetta;

$cobalt = new Car("Cobalt", "Blue", 3000);
$cars[] = $cobalt;

// this is how you access variables
echo $cars[0]->model; 
echo $cars[0]->color;
echo $cars[0]->price;

?>

您的语法和构造函数错误

以下是最终代码:

<?php

class Car{
    // the variables should be public
    public $model;
    public $color;
    public $price;
    // this is how you write a constructor
    public function __construct($model, $color, $price)
    {
        // this is how you set instance variables
        $this->model = $model;
        $this->color = $color;
        $this->price = $price;
    }
}

$cars = [];
$jetta = new Car("Jetta", "Red", 2500);
$cars[] = $jetta;

$cobalt = new Car("Cobalt", "Blue", 3000);
$cars[] = $cobalt;

// this is how you access variables
echo $cars[0]->model; 
echo $cars[0]->color;
echo $cars[0]->price;

?>

您的代码中有多个错误,我已经用箭头指出了它们
◄■■■

<?php

class Car{
    public $model;   //◄■■■■■■■■■■ IF PRIVATE YOU WILL NOT
    public $color;   //◄■■■■■■■■■■ BE ABLE TO ACCESS THEM
    public $price;   //◄■■■■■■■■■■ FROM OUTSIDE.
    public function __construct ($model, $color, $price) //◄■■■ CONSTRUCT
    {
        $this->model = $model;   //◄■■■■■■■■■■■■■■■■■■■■■■■ NOT THIS.$
        $this->color = $color;   //◄■■■■■■■■■■■■■■■■■■■■■■■ NOT THIS.$
        $this->price = $price;   //◄■■■■■■■■■■■■■■■■■■■■■■■ NOT THIS.$
    }
}

$cars = [];
$jetta = new Car("Jetta", "Red", 2500);
$cars[] = $jetta;

$cobalt = new Car("Cobalt", "Blue", 3000);
$cars[] = $cobalt;

// this is the part of the code that doesn't work
// I need to output the values from the objects, model, color and price
echo $cars[0]->model;   //◄■■■■■■■■■■■■■■■■■■ PUBLIC PROPERTY WITHOUT $.
echo $cars[0]->color;   //◄■■■■■■■■■■■■■■■■■■ PUBLIC PROPERTY WITHOUT $.
echo $cars[0]->price;   //◄■■■■■■■■■■■■■■■■■■ PUBLIC PROPERTY WITHOUT $.
?>

您的代码中有多个错误,我已经用箭头指出了它们
◄■■■

<?php

class Car{
    public $model;   //◄■■■■■■■■■■ IF PRIVATE YOU WILL NOT
    public $color;   //◄■■■■■■■■■■ BE ABLE TO ACCESS THEM
    public $price;   //◄■■■■■■■■■■ FROM OUTSIDE.
    public function __construct ($model, $color, $price) //◄■■■ CONSTRUCT
    {
        $this->model = $model;   //◄■■■■■■■■■■■■■■■■■■■■■■■ NOT THIS.$
        $this->color = $color;   //◄■■■■■■■■■■■■■■■■■■■■■■■ NOT THIS.$
        $this->price = $price;   //◄■■■■■■■■■■■■■■■■■■■■■■■ NOT THIS.$
    }
}

$cars = [];
$jetta = new Car("Jetta", "Red", 2500);
$cars[] = $jetta;

$cobalt = new Car("Cobalt", "Blue", 3000);
$cars[] = $cobalt;

// this is the part of the code that doesn't work
// I need to output the values from the objects, model, color and price
echo $cars[0]->model;   //◄■■■■■■■■■■■■■■■■■■ PUBLIC PROPERTY WITHOUT $.
echo $cars[0]->color;   //◄■■■■■■■■■■■■■■■■■■ PUBLIC PROPERTY WITHOUT $.
echo $cars[0]->price;   //◄■■■■■■■■■■■■■■■■■■ PUBLIC PROPERTY WITHOUT $.
?>

去查什么是
private
visibility意思。去查什么是
private
visibility意思。谁投了反对票,请再次检查答案。谁投了反对票,请再次检查答案。非常感谢解释性评论,这很有帮助。非常感谢解释性评论,这很有帮助。