2

Recently, I have been trying to object orient my PHP.

I understand OOP, but for some reason, I am having trouble when it comes to implementing it with Php.

I feel as though it isn't making sense. For instance, lets say I have a Friend class. It has many methods such as :

1) getName

2) getAge

3) sendMessage

etc..

When the user loads the webpage, I would populate an array of friend objects from the database. This would be all the users friends.

So far, it makes sense. But, let's say I make an ajax request to get information on a friend. The data is no longer available, correct?

This is where I am confused. I can't create the application thinking that I can always access these objects. So, how would this typically work?

Would it make sense not to store the friends as objects, and instead just have functions such as getFriend() or getFriendAge() which would take in the friendID and pull the data from the database?

I'm used to objects always existing, whereas in Php they only exist on the initial load of the webpage. My site uses lots of ajax.

I'm having difficulty putting this into words, hopefully my confusion will be cleared up. Thanks!

tereško
  • 58,060
  • 25
  • 98
  • 150
Wyatt
  • 445
  • 3
  • 12
  • 3
    I'll assume you're coming from JavaScript world where objects always exist. With any web application that's based on HTTP, those objects aren't persisted. They're re-created on every request (various languages and stacks optimize this step) - whether it's done via sessions or passing an identifier of some sort then pulling the data out - the objects you want need to be re-created. There are frameworks and coding practices that do most of this for you, so it appears to you as if it always exists. The thing you're after is called a session. – Mjh Jul 21 '17 at 12:02
  • 1
    @Mjh your comment must be written as answer – Neodan Jul 21 '17 at 12:06
  • I don't think your confusion is with PHP, but rather with the higher level concept of distributed computing and the client-server model. – Joshua Jones Jul 21 '17 at 12:09
  • 1
    When your webserver print's an output in PHP, it destroys the runtime. All your objects are gone. – tereško Jul 21 '17 at 12:12

1 Answers1

1

You have 2 options:

  1. Have all your "friend" models implement JsonSerializable and preload all data in JS variables. This has the advantage that everything is pre-loaded once and it then rests on the client-side whenever it's neede. However this can be a lot of data taking up memory:

    <?php 
    $friends = $db->query("SELECT * FROM people JOIN friends WHERE friends.source_id='some user whos logged in'");
    ?>
    <script>
         window.friends = <?= json_encode($friends); ?>;
         //friends can be used in your scripts within this page
    
  2. Create an api. The user only gets what they need dynamically, however there's always a return-trip latency for each request making this a bit slower than pre-loading the data:

    <script>
         window.currentUserId= <?= some user whos logged in ?>;
         $.ajax({
             url: "/friends.php",
             data: {
                 source: currentUserId,
                 target: friendId
             }
         }); 
    </script>
    

    Somewhere in file friends.php

     <?php 
     $sourceId = filter_input(INPUT_GET,"source");
     $target = filter_input(INPUT_GET,"target"); //Variable names are hard
    
     //Verify that current user can see the friends of $sourceId here
    
     $friendQ = $db->prepare("SELECT * FROM people JOIN friends WHERE friends.source_id=?" and friends.target_id=?); 
     $friendQ->bindParam(1, $sourceId); //Assume this is PDO, it really doesn't matter
     $friendQ->bindParam(2, $target); 
     if ($friendQ->execute()) {
         //Get results and build your model object
         echo json_encode($friend);
         die();
     } else {
         http_status_code(500);
         die();
     }
    

The point to take from this is that you should load the minimum amount of data required by each scenario. How you'll serve your page and data to your users really depends on who your users are and what is more important to them and you, in the end it's up to you and your app requirements.

apokryfos
  • 38,771
  • 9
  • 70
  • 114
  • So, I guess my confusion is why people use OOP with Php. – Wyatt Jul 21 '17 at 12:50
  • If you just need a database access API then OOP might not be necessary, at least not on the PHP end. People use OOP in PHP because they usually want PHP to do more things with the data and OOP allows them to write cleaner code that is easier to maintain. – apokryfos Jul 21 '17 at 12:53