duanmu8911 2017-09-28 07:28
浏览 26
已采纳

PHP数组仅返回值

I've begun study of PHP Object oriented programming.
I have limited experience with PHP in general.

I have PHP code as follows:

A "Cars" class:

class Cars {
    static $car_specs = array("wheel_count", "name", "color");
    static function set_car_detail() {
        self::$car_specs["wheel_count"] = 4;
        self::$car_specs["name"] = "default";
        self::$car_specs["color"] = "generic";
    }
    static function get_car_detail() {
        return self::$car_specs;
    }
}

and then I am trying to output this:

Cars::set_car_detail();
echo implode(',', Cars::get_car_detail());

the echo is :

wheel_count,name,color,4,default,generic

however I'm trying to get:

4,default,generic

foreach attempts yielded similar responses. I wish to understand what is it I am doing wrong.

  • 写回答

4条回答 默认 最新

  • du6jws6975 2017-09-28 08:39
    关注

    The code you posted is not a class at all. It is just a bunch of global functions and variables with funky names. Try to avoid static properties and methods as they are not OOP. Also try to avoid public properties and getters/setters as they are just procedural programming in disguise.

    A skeleton of a Car class could look like this:

    class Car {
        private $wheel_count;
        private $name;
        private $color;
    
        public function __construct($wheel_count, $name, $color)
        {
            $this->wheel_count = $wheel_count;
            $this->name = $name;
            $this->color = $color;
        }
    
        public function paint($new_color)
        {
            $this->color = $new_color;
        }
    
        public function describe()
        {
            return sprintf('%d wheels, %s %s', $this->wheel_count, $this->color, $this->name);
        }
    }
    

    The object properties are private. Encapsulation is one of the key concepts of OOP. They are set in the constructor. The role of the constructor is to initialize the object, to make it ready to work. Make sure the constructor initializes all object's properties and it doesn't do anything else (no work in the constructor.)

    Create methods in the class for the actions that make sense for your class. For example, the number of wheels of a car never changes, there is no point in writing a method to set a different number of wheels. But the color of the car sometimes changes and the paint() method handles that.

    Avoid writing "getters" (i.e. methods that do nothing than returning the value of a property). They are the sign that some code that belongs to the class is written somewhere outside the class (and many times it is duplicated here and there and everywhere, with minor or no changes). Whenever is possible and appropriate, write a method that uses the object's properties to generate an useful value (as opposed to writing getters to let the useful value be computed somewhere else).

    This is how the Car class described below is used:

    $car1 = new Car(4, 'Mazda', 'red');
    $car2 = new Car(8, 'big truck', 'blue');
    
    echo($car1->describe());
    # 4 wheels, red Mazda
    
    $car1->paint('green');
    echo($car1->describe());
    # 4 wheels, green Mazda
    

    展开全部

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部