0

I want to have an array with objects. For example.

public $aCarObjects = array();
$aCars = array('Audi','BMW','Ford');

foreach($aCars as $car){
  array_push($aCarObjects, new Car($car));
}

// Simplified class
class Car implements C{
   private $carName;

  public function __construct($carName){
     $this->carName = $carName;
  } 
}
interface C {}

This is a very shortened version of what I am trying. The class Car contains some info about the car.

When I am using the Interface C in the Car class. I cannot add objects to the array. Why is that so?

9
  • I found something that might help you -> clik Even it's same example Commented Nov 19, 2018 at 22:35
  • 2
    Yes, you can use "new" inside array_push. A shorthand version (just a simpler syntax) is $aCarObjects[] = new Car($car); Commented Nov 19, 2018 at 22:37
  • Is there anything wrong with your code? Anything not working as expected? Commented Nov 19, 2018 at 22:38
  • 1
    @Jeff array_push isn't the same as $array[] - it only does the same thing in most cases Commented Nov 19, 2018 at 22:40
  • 2
    Use modern syntax: $arrray[] replace array_push($array) since at least 4 years! It hurt to see Commented Nov 19, 2018 at 22:46

1 Answer 1

3

This error has nothing to do with arrays, it's a class hoisting bug (feature?) which is fixed by moving your class definition above the call to new Car. Apparently, PHP does not hoist class definitions if they implement an interface.

Here's a minimal example of the phenomenon.

Works:

new Foo();
class Foo {}
interface Bar {} 

Doesn't:

new Foo(); # <-- error: Class 'Foo' not found
class Foo implements Bar {}
interface Bar {} 

Perhaps this is a candidate for PHP Sadness?

Here's a working version of your code that also addresses a stray public keyword in line 1:

interface C {}

class Car implements C {
    private $carName;

    public function __construct($carName) {
        $this->carName = $carName;
    }
}

$aCarObjects = [];
$aCars = ['Audi', 'BMW', 'Ford'];

foreach ($aCars as $car) {
    array_push($aCarObjects, new Car($car));
}

print_r($aCarObjects);

Output:

Array
(
    [0] => Car Object
        (
            [carName:Car:private] => Audi
        )

    [1] => Car Object
        (
            [carName:Car:private] => BMW
        )

    [2] => Car Object
        (
            [carName:Car:private] => Ford
        )

)

Try it!

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks for the comment. The construct part was a typo the other things are nice to know. My problem is the interface, which I did not mention.
The issue is that you've declared the class after the call to new Car. Move the class definition to the top of your file; it's not hoisted if it implements an interface, which is pretty strange.
That was really it! Thanks so much for all the answers!

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.