Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

PHP Designing Interfaces in PHP Introducing Interfaces Implementing an Interface

Alex Bauer
Alex Bauer
9,426 Points

Can someone help me understand Interfaces in PHP?

I have a screenshot of my code here: https://w.trhou.se/obdnvecaci

My Questions:

  1. What is “posts” in index.php? I thought it had to be a property because in jsonRepository when we return $data->$entity I know that “$entity” is referring to “posts” when the object was instantiated.
  2. The foreach function in jsonRepository really confuses me. I’ve read in the documentation that json_decode returns the value of that which was read into an “appropriate PHP type”; meaning to me that it was an array thus allowing us to use it in the foreach loop as an array. Second, the if statement; what is $field in “$data->$field”? Again, this must mean that it’s a property or a method because of this “->” indication but I can’t understand what it could mean or when it was defined. Also, I don’t even know why there’s an if statement used to allow the method to work if the 3rd parameter wasn’t even used in the original call to the method? (i.e var_dump($repo->find('posts', 1));)

P.S. If I can get any help I'd greatly appreciate it even if it doesn't answer all of my questions. I appreciate any effort and time anyone could give. Thank you.

I've read the code you posted and here are the answers to your questions:

1 - the 'posts' in index.php is the section of the json where the repository class will look for data. In this abstraction the original programmer made as if the database.json was a proper database and every key in it were a table, as every key contains an array of objects which would be interpreted as rows in this table abstraction. To understand what $data->$entity means you must have two concepts clear: one - json_decode returns a stdclass by defalut and an array if you pass true to the second and optional parameter. So, as the code does json_decode returns an object to the $data. seconde - if you would do $data[$entity] you would be accessing the $entity key inside the array, the same goes for objects, when you use $data->$entity you are accessing the $entity property from the $data object. You can still do this to call methods like this: $method = "find"; $repo->$method();

2 - explaining this function is now easier now that you know that json_decode returns an object, inside the foreach it will loop throgh each $key of your json_decode, or in this abstraction, for each 'table' of your json database. So basically, what this functions does is: it FINDs the data that has a value matching the passed $value on the given $field, which defaults to 'id'. So, if you want to search a post by the 'primary key' of your json database, you would do: $repo->find('posts',1); OR $repo->find('posts',1,'id');

But if you wanted to find a page with the slug home, how would you do it?

$repo->find("pages", "home", "slug"); "pages" is your entity, or table "home" is the value you are looking for "id"is the field

If you wanted to write a mysql repository for that, you would implement those two methods as:

public function getConnection(){ return new PDO("mysql:host=localhost;dbname=database", "root", "root"); }

public function all($entity){ $con = $this->getConnection(); $sth = $con->prepare("SELECT * FROM $entity"); return $sth->fetchAll(); }

public function find($entity, $value, $field = 'id'){ $con = $this->getConnection(); $sth = $con->prepare("SELECT * FROM $entity WHERE $field = ?"); $sth->bindParam(1,$value); return $sth->fetchAll(); }

hope that helped!

2 Answers

hey there, no problem

so, javascript isn't a requirement for this code it's just json

think of json like a csv or a serialized object, it's just a format to store data, javascript uses it a lot, but it's not part of it good luck on your journey

Alex Bauer
Alex Bauer
9,426 Points

Thank you, good luck to you too :D

Alex Bauer
Alex Bauer
9,426 Points

Yes, this helps SO much thank you! I haven't began to learn javascript or json yet so I think that's what was confusing me. I REALLY appreciate your response and your time, I'll come back to this once I learn javascript and then I think I would be able to interpret the code.