2014-05-24 19:09
浏览 32


I'm studying how objects and arrays work in PHP for a little project. Unfortunately it's difficult to find out what are good ways to deal with data, there are many solutions but i can't find the right solution for my problem. I think I prefer two work with an object.

I have two questions related to this example:

class cars {}

$cars = new cars();  

$cars->brand = new cars();
$cars->brand->audi = "A6";
$cars->brand->opel = "insignia";

question 1 How can I get each brand as output for example with a for each loop or other function?

echo somesolution will give audi

question 2 Is there a way like with arrays using parentheses to write the code without repeating $cars->brand->opel ?

question 3 Why do I need tho create the class cars?

图片转代码服务由CSDN问答提供 功能建议

我正在研究对象和数组如何在PHP中为一个小项目工作。 不幸的是,很难找到处理数据的好方法,有很多解决方案,但我找不到适合我的问题的解决方案。 我想我更喜欢用对象做两件事。


  class cars {} 
 $ cars = new cars();  
 $ cars-> brand = new cars(); 
 $ cars-> brand-> audi =“A6”; 
 $ cars-> brand-> opel =“insignia”;  

问题1 如何以每个循环或其他功能为例,如何将每个品牌作为输出? \ n

echo somesolution将给出audi

问题2 是否有类似于使用括号的数组编写代码而不重复 $ cars-> brand-> opel

问题3 为什么我需要创建班车?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

3条回答 默认 最新

  • dongqiongjiong4740 2014-05-24 19:55

    No matter your way is absolutely possible and of course, not so hard to make much more generic, it is as well very incorrect in the terms of Object oriented design.

    Brands should be a totally different object. In your design you inject a brand new instance of Cards to the current Cars instance, only to specify properties. For this purpose you can use standard class object stdClass.

    With foreach around your Brand object (cars), you will get as keys the property names audi, opel, etc. And as values their models.

    foreach ($cars->brand as $name => $value) {
        echo $name . ' ' . $value . "<br />";

    which will result in audi a6, opel insignia.

    However, the thing you are looking for is the polymorphism. Each Brand is object of type Car, unless it's a brand of something else. This is the way which people might tell you it's the right one. Because when designing an application, you are seeking a good design as well as the work should be done.

    Each object, Audi, Opel, etc is a child of a superclass Car. Thus, you instantiate them for a given parameter.

    abstract class Car {
        protected $_name;
        protected $_model;
        public function __construct($model) {
            $this->_model = $model;
        public function getName() {
            return $this->_name;
        public function getModel() {
            return $this->_model;
    class Audi extends Car { protected $_name = 'Audi'; }
    class Opel extends Car { protected $_name = 'Opel'; }
    class CarsFactory {
        public static function createCars($name, $model) {
            switch($name) {
                case 'Audi':
                    return new Audi($model);
                case 'Opel':
                    return new Opel($model);
    $cars[] = CarsFactory::createCars('Audi', 'A6');
    $cars[] = CarsFactory::createCars('Opel', 'Vectra');
    $cars[] = CarsFactory::createCars('Opel', 'insignia');
    foreach ($cars as $car) {
        echo $car->getName() . ' ' . $car->getModel() . "<br/>";

    which results into

    Audi A6
    Opel Vectra
    Opel insignia

    Another way is to inject new Model object into the cars object. This is almost what you have done, but your brand property will not recieve the superclass, but a Model class (or Brand class)

    This way, you can provide interface for iterating or for just printing, in your both objects. Model and Car.

    class Car {
         * @var Model
        private $_models;
        public function setModel(Model $model) {
            $this->_models[] = $model;
        public function getModels() {
            return $this->_models;
        public function getModelsByCar($car) {
            foreach ($this->getModels() as $model) {
                if ($model->getCarName() == $car) {
                    $models[] = $model;
            return $models;
        public function getCarByModel($modelName) {
            foreach ($this->getModels() as $model) {
                if ($model->getModelname() == $modelName) {
                    return $model->getCarName();
    class Model {
        private $_carName;
        private $_modelName;
        public function __construct($name, $model) {
            $this->_carName = $name;
            $this->_modelName = $model;
        public function getCarName() {
            return $this->_carName;
        public function getModelName() {
            return $this->_modelName;
    $cars = new Car();
    $cars->setModel(new Model('Audi', 'A6'));
    $cars->setModel(new Model('Audi', 'V8'));
    $cars->setModel(new Model('Audi', 'A8'));   
    $cars->setModel(new Model('Opel', 'insignia'));
    $cars->setModel(new Model('Opel', 'vectra'));
    foreach ($cars->getModels() as $model) {
        echo $model->getCarName() . ' ' .$model->getModelName() . "<br/>";
    echo "-----------------------------<br/>";
    foreach ($cars->getModelsByCar('Audi') as $model) {
        echo $model->getModelName() . "<br />";
    echo "-----------------------------<br/>";
    echo $cars->getCarByModel('insignia');


    Audi A6
    Audi V8
    Audi A8
    Opel insignia
    Opel vectra
    点赞 打赏 评论
  • dshdsh2016 2014-05-24 19:15

    Accessing attributes without having them in the class is so Javascript.

    What you probable need is an array:

    $cars = array(); // Or [] in PHP 5.4 and greater
    $cars['brand']['audi'] = 'A6';
    $cars['brand']['opel'] = 'Insignia';
    // Printing
    foreach($cars['brand'] as $brand => $model) {
        echo $brand . ': '. $model;
    点赞 打赏 评论
  • dsxcv5652 2014-05-24 19:51

    answer #1:

    PHP5 provides a way for objects iterate through a list of items, e.g.: foreach statement http://www.php.net/manual/en/language.oop5.iterations.php

    • Simple:
    foreach($object as $key => &$value){

    also you can use for your objects:

    answer #2:

    see arrays: http://php.net/manual/en/language.types.array.php

    $cars->brand = array(
                        'audi' => 'A6',
                        'opel' => 'insignia',
    foreach($cars->brand as $name => $model){...}

    answer #3:

    use new stdClass(); if you want to use an anonymous object

    点赞 打赏 评论

相关推荐 更多相似问题