0

I'm not too experienced with PHP, but I'm trying to create a small program that will utilize a class schedule and print out statements based on what time each class is, etc.

Here is my program that I have a question about:

class Period {
    public $name;
    public $times = array(); //Ideally, whatever is inputted here will be an array.

    public function __construct($name, $times) {
        $this->name = $name;
        $this->times = $times;
    }
}
$A_Block = new Period("AP Graphic Design", ("Day 1"=>"8:20", "Day 2"=>"11:35", "Day 4"=>"2:10", "Day 5"=>"10:20","Day 7"=>"1:10"));

I have the period class with two attributes, a simple name string, and an associative array (dictionary) of times that the class occurs. During the initialization of $A_Block, something is wrong with how I'm creating the array.

What will I need to change in my Period class in order to be able to create the A_Block class with the associative array?

Thanks!

0

1 Answer 1

1

Your array syntax is wrong: that should be:

$A_Block = new Period("AP Graphic Design", array("Day 1"=>"8:20", ... ));

Or, using the newer array syntax:

$A_Block = new Period("AP Graphic Design", ["Day 1"=>"8:20", .... ]);
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks! Can't believe the issue was that simple. For some reason I was thinking that having array() while creating the object was incorrect.
Glad to help! :-) You can read the gory details here.
I meant to add that, if the second parameter in the constructor is always an array, you can use a type hint: public function __construct($name, array $times) { ... } - this will throw an exception if anything but an array is passed in. Good habits and all that...

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.