How to create an array with objects in php
up vote
0
down vote
favorite
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?
php arrays oop object
|
show 4 more comments
up vote
0
down vote
favorite
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?
php arrays oop object
I found something that might help you -> clik Even it's same example
– SilvioCro
Nov 19 at 22:35
2
Yes, you can use "new" inside array_push. A shorthand version (just a simpler syntax) is$aCarObjects = new Car($car);
– Jeff
Nov 19 at 22:37
Is there anything wrong with your code? Anything not working as expected?
– Jeff
Nov 19 at 22:38
1
@Jeffarray_push
isn't the same as$array
- it only does the same thing in most cases
– Philipp
Nov 19 at 22:40
2
Use modern syntax: $arrray replace array_push($array) since at least 4 years! It hurt to see
– Cryptopat
Nov 19 at 22:46
|
show 4 more comments
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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?
php arrays oop object
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?
php arrays oop object
php arrays oop object
edited Nov 19 at 22:54
asked Nov 19 at 22:31
Jake
3511
3511
I found something that might help you -> clik Even it's same example
– SilvioCro
Nov 19 at 22:35
2
Yes, you can use "new" inside array_push. A shorthand version (just a simpler syntax) is$aCarObjects = new Car($car);
– Jeff
Nov 19 at 22:37
Is there anything wrong with your code? Anything not working as expected?
– Jeff
Nov 19 at 22:38
1
@Jeffarray_push
isn't the same as$array
- it only does the same thing in most cases
– Philipp
Nov 19 at 22:40
2
Use modern syntax: $arrray replace array_push($array) since at least 4 years! It hurt to see
– Cryptopat
Nov 19 at 22:46
|
show 4 more comments
I found something that might help you -> clik Even it's same example
– SilvioCro
Nov 19 at 22:35
2
Yes, you can use "new" inside array_push. A shorthand version (just a simpler syntax) is$aCarObjects = new Car($car);
– Jeff
Nov 19 at 22:37
Is there anything wrong with your code? Anything not working as expected?
– Jeff
Nov 19 at 22:38
1
@Jeffarray_push
isn't the same as$array
- it only does the same thing in most cases
– Philipp
Nov 19 at 22:40
2
Use modern syntax: $arrray replace array_push($array) since at least 4 years! It hurt to see
– Cryptopat
Nov 19 at 22:46
I found something that might help you -> clik Even it's same example
– SilvioCro
Nov 19 at 22:35
I found something that might help you -> clik Even it's same example
– SilvioCro
Nov 19 at 22:35
2
2
Yes, you can use "new" inside array_push. A shorthand version (just a simpler syntax) is
$aCarObjects = new Car($car);
– Jeff
Nov 19 at 22:37
Yes, you can use "new" inside array_push. A shorthand version (just a simpler syntax) is
$aCarObjects = new Car($car);
– Jeff
Nov 19 at 22:37
Is there anything wrong with your code? Anything not working as expected?
– Jeff
Nov 19 at 22:38
Is there anything wrong with your code? Anything not working as expected?
– Jeff
Nov 19 at 22:38
1
1
@Jeff
array_push
isn't the same as $array
- it only does the same thing in most cases– Philipp
Nov 19 at 22:40
@Jeff
array_push
isn't the same as $array
- it only does the same thing in most cases– Philipp
Nov 19 at 22:40
2
2
Use modern syntax: $arrray replace array_push($array) since at least 4 years! It hurt to see
– Cryptopat
Nov 19 at 22:46
Use modern syntax: $arrray replace array_push($array) since at least 4 years! It hurt to see
– Cryptopat
Nov 19 at 22:46
|
show 4 more comments
1 Answer
1
active
oldest
votes
up vote
2
down vote
accepted
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!
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.
– Jake
Nov 19 at 22:58
1
The issue is that you've declared the class after the call tonew Car
. Move the class definition to the top of your file; it's not hoisted if it implements an interface, which is pretty strange.
– ggorlen
Nov 19 at 23:18
1
That was really it! Thanks so much for all the answers!
– Jake
Nov 21 at 20:48
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383587%2fhow-to-create-an-array-with-objects-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
accepted
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!
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.
– Jake
Nov 19 at 22:58
1
The issue is that you've declared the class after the call tonew Car
. Move the class definition to the top of your file; it's not hoisted if it implements an interface, which is pretty strange.
– ggorlen
Nov 19 at 23:18
1
That was really it! Thanks so much for all the answers!
– Jake
Nov 21 at 20:48
add a comment |
up vote
2
down vote
accepted
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!
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.
– Jake
Nov 19 at 22:58
1
The issue is that you've declared the class after the call tonew Car
. Move the class definition to the top of your file; it's not hoisted if it implements an interface, which is pretty strange.
– ggorlen
Nov 19 at 23:18
1
That was really it! Thanks so much for all the answers!
– Jake
Nov 21 at 20:48
add a comment |
up vote
2
down vote
accepted
up vote
2
down vote
accepted
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!
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!
edited Nov 19 at 23:23
answered Nov 19 at 22:41
ggorlen
6,1763825
6,1763825
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.
– Jake
Nov 19 at 22:58
1
The issue is that you've declared the class after the call tonew Car
. Move the class definition to the top of your file; it's not hoisted if it implements an interface, which is pretty strange.
– ggorlen
Nov 19 at 23:18
1
That was really it! Thanks so much for all the answers!
– Jake
Nov 21 at 20:48
add a comment |
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.
– Jake
Nov 19 at 22:58
1
The issue is that you've declared the class after the call tonew Car
. Move the class definition to the top of your file; it's not hoisted if it implements an interface, which is pretty strange.
– ggorlen
Nov 19 at 23:18
1
That was really it! Thanks so much for all the answers!
– Jake
Nov 21 at 20:48
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.
– Jake
Nov 19 at 22:58
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.
– Jake
Nov 19 at 22:58
1
1
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.– ggorlen
Nov 19 at 23:18
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.– ggorlen
Nov 19 at 23:18
1
1
That was really it! Thanks so much for all the answers!
– Jake
Nov 21 at 20:48
That was really it! Thanks so much for all the answers!
– Jake
Nov 21 at 20:48
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53383587%2fhow-to-create-an-array-with-objects-in-php%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
I found something that might help you -> clik Even it's same example
– SilvioCro
Nov 19 at 22:35
2
Yes, you can use "new" inside array_push. A shorthand version (just a simpler syntax) is
$aCarObjects = new Car($car);
– Jeff
Nov 19 at 22:37
Is there anything wrong with your code? Anything not working as expected?
– Jeff
Nov 19 at 22:38
1
@Jeff
array_push
isn't the same as$array
- it only does the same thing in most cases– Philipp
Nov 19 at 22:40
2
Use modern syntax: $arrray replace array_push($array) since at least 4 years! It hurt to see
– Cryptopat
Nov 19 at 22:46