Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create array of objects in php

I'm attempting to create an array of objects in php and was curious how I would go about that. Any help would be great, thanks!

Here is the class that will be contained in the array

<?php

class hoteldetails {
private $hotelinfo;
private $price;

public function sethotelinfo($hotelinfo){
    $this->hotelinfo=$hotelinfo;
}

public function setprice($price){
    $this->price=$price;
}

public function gethotelinfo(){
    return $hotelinfo;
}

public function getprice(){
    return $price;
}

}

And here is what I am attempting to do-

<?PHP
include 'file.php';

$hotelsdetail=array();    

$hotelsdetail[0]=new hoteldetails();
$hotelsdetail[0].sethotelinfo($rs);
$hotelsdetail[0].setprice('150');


?>

The class attempting to create the array doesn't compile but is just a best guess as to how I can do this. Thanks again

like image 372
Alex Miles Avatar asked Nov 24 '13 12:11

Alex Miles


People also ask

How do you create an array of objects in PHP?

We can use the array() function to create an array of objects in PHP. The function will take the object as the arguments and will create an array of those objects. We can create objects by creating a class and defining some properties of the class. The properties of the class will have some values.

How can we store objects in array in PHP?

The serialize() function in PHP can be used before storing the object, and the unserialize() function can be called when the object needs to be retrieved from the session. The function converts a storable representation of a specific value into a sequence of bits.

Can we create array of objects?

Creating an Array Of Objects In Java – An Array of Objects is created using the Object class, and we know Object class is the root class of all Classes. We use the Class_Name followed by a square bracket [] then object reference name to create an Array of Objects.

Can you create objects in PHP?

To create an Object in PHP, use the new operator to instantiate a class. If a value of any other type is converted to an object, a new instance of the stdClass built-in class is created.


2 Answers

What you should probably do is:

$hotelsDetail = array();

$details = new HotelDetails();
$details->setHotelInfo($rs);
$details->setPrice('150');

// assign it to the array here; you don't need the [0] index then
$hotelsDetail[] = $details;

In your specific case, the issue is that you should use ->, not .. The period isn't used in PHP to access attributes or methods of a class:

$hotelsdetail[0] = new hoteldetails();
$hotelsdetail[0]->sethotelinfo($rs);
$hotelsdetail[0]->setprice('150'); 

Note that I capitalized the class, object, and function names properly. Writing everything in lowercase is not considered good style.

As a side note, why is your price a string? It should be a number, really, if you ever want to do proper calculations with it.

like image 188
slhck Avatar answered Oct 01 '22 23:10

slhck


You should append to your array, not assign to index zero.

$hotelsdetail = array();    
$hotelsdetail[] = new hoteldetails();

This will append the object to the end of the array.

$hotelsdetail = array();    
$hotelsdetail[] = new hoteldetails();
$hotelsdetail[] = new hoteldetails();
$hotelsdetail[] = new hoteldetails();

This would create an array with three objects, appending each one successively.


Additionally, to correctly access an objects properties, you should use the -> operator.

$hotelsdetail[0]->sethotelinfo($rs);
$hotelsdetail[0]->setprice('150');
like image 21
Bryant James Avatar answered Oct 01 '22 21:10

Bryant James