duanmu8911 2017-09-28 15: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 16: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条)

报告相同问题?

悬赏问题

  • ¥20 腾讯企业邮箱邮件可以恢复么
  • ¥15 有人知道怎么将自己的迁移策略布到edgecloudsim上使用吗?
  • ¥15 错误 LNK2001 无法解析的外部符号
  • ¥50 安装pyaudiokits失败
  • ¥15 计组这些题应该咋做呀
  • ¥60 更换迈创SOL6M4AE卡的时候,驱动要重新装才能使用,怎么解决?
  • ¥15 让node服务器有自动加载文件的功能
  • ¥15 jmeter脚本回放有的是对的有的是错的
  • ¥15 r语言蛋白组学相关问题
  • ¥15 Python时间序列如何拟合疏系数模型