0

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
  • 2
    This is not OOP- you should use getter/setter instead of putting every property into 1 array + also no need for static Commented Sep 28, 2017 at 15:30
  • Possible duplicate of PHP - Merge two arrays (same-length) into one associative? Commented Sep 28, 2017 at 15:35
  • Why do you set the details in a separate function? In the future, are you planning to pass in those values? Otherwise, it does not make sense as to why you would not create the already hydrated array. Commented Sep 28, 2017 at 15:41
  • The Cars "class" is not a class at all. It works more like a namespace. Avoid static members and methods. They are global objects with fancy names. Commented Sep 28, 2017 at 16:14

4 Answers 4

2

You have initialized the array with values, but perhaps you want to initialize it with keys:

static $car_specs = array(
    "wheel_count" => null, 
    "name" => null, 
    "color" => null
);
Sign up to request clarification or add additional context in comments.

2 Comments

I see how this working but unsure why compare to what I wrote? I assume I am doing a bad practice when I initialize an array with just keys and later add them values?
@AviE.Koenig: Sure, you are right. Just it is one of possible answers for the question "what is it I am doing wrong".
1

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

2 Comments

Thank you.your reply was most informative. I do agree that the class provided was not really a class...my focus was on that time on how static variables and functions work.
Static properties are just global variables with complicate names (and visibility constraints). They are not inherited; you can access a static variable (Cars::$car_specs) from a class that extends Cars using self::$car_specs but, unless you (re-)declare the property in the child class, self::$car_specs in the child class is the same thing as Cars::$car_specs. This is a counter-intuitive behaviour that leads to confusion and errors. Static methods are just global functions with complicate names (and visibility constraints).
1

Since you already have an array of key values, why don't you create a corresponding array of values for each key and use array_combine().

class Cars {
    static $car_specs;

    static function set_car_detail() {
        $keys = array("wheel_count", "name", "color");
        $values = array(4, "default", "generic");

        self::$car_specs = array_combine($keys, $values);
    }

    static function get_car_detail() {
        return self::$car_specs;
    }
}

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

Comments

0

There's a self-explanatory function specifically for that

   static function get_car_detail() {
      return array_values(self::$car_specs);
   }

1 Comment

implode already operates only on array values, that's not what the problem is here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.