0

I have objects in an Array. I want to access those objects' properties, but I'm not having any luck.

class Article {
    public $category;
    public $title;
    public $img;
    public $location;
    public $text;


    public function __construct($category, $title, $img, $location, $text) {
        $this->category = $category;
        $this->title = $title;
        $this->img = $img;
        $this->location = "pages/" . $location;
        $this->text = $text;
    }
}

//db of articles
$runawayFive = new Article("news", "The Runaway Five Comes to Fourside",     "img/BluesBrothers.jpg",
 "runaway_five_comes_to_fourside.html",
 "The Runway Five continues its nationwide tour, stopping in Fourside to    perform at the world famous Topolla Theater. Previously, an unexpected delay caused the group to postpone its shows for over a week. The award-winning group was forced to speed ten days in neighboring town, Threed. Tunnels going to and from Threed were blocked, but no one really knows how or why." . "<br>"
 ."The Runaway Five will being playing at the Topolla Theater during Friday and Saturday night. Tickets are $20 per adult."
);
$articles = Array($runawayFive);
echo $articles[0]["title"];

I should have the title of the article echoed out, but I'm not getting anything. I can do var_dump($articles[0]) and return the object, but can't access its values.

5 Answers 5

2

In PHP you access object properties using the -> operator.

echo $articles[0]->title;
Sign up to request clarification or add additional context in comments.

Comments

0

Try this

echo $articles[0]->title;

More info and examples could be found here http://php.net/manual/en/language.types.object.php

1 Comment

Looks like Conde beat you to it.
0

You can access the object property directly like this

echo $runawayFive->title;

No need for an array conversion

Comments

0
$articles

It's an array, this array has only 1 value, and the only value you have in the arrray it's an object from type Article.

array(
    0 => new Article()
);

You can reference to each value of an array by the key, the key by default it's numeric index starting from zero. So you can access to the array by

$articles[ $indexValue ];

In this case you can have something like:

$article = $articles[ 0 ];

To access the value of the array in the index zero. So in this case this is an object. So to access to the non static methods or instance varialbes of an object you use the -> operator. Like follows:

$article->title;

The short sintax is:

$articles[0]["title"];

And a nicer one:

$article = $articles[0];
$article->title;

For output the value just write echo before the call to the instance variable.

Like:

 $article = $articles[0];
 echo $article->title;

OR

 echo $articles[0]->title;

Comments

0

Yep, you have to access the properties like this:

$articles[0]->title;

Or if you really want to access the properties in array format you can implement 'ArrayAccess' like below:

class Article implements ArrayAccess {

 public $title;

}

$article = new Article();
echo $article['title'];

For reference:

http://php.net/manual/en/class.arrayaccess.php http://php.net/manual/en/language.oop5.interfaces.php

Comments

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.