Jump to content

Greetings viewers!

 

So over the past 6 months, I've been playing with PHP (mostly procrastinating and filling the gaps between classes) and I feel fairly comfortable with it. Except that I use it largely functionally and not as an object-oriented language. Although I have a fair understanding of why you'd be inclined to create PHP classes (personally, I'd use them for storing a load of static variables and methods) I'm not really sure under what circumstances I'd create objects.

 

More background: In Java or C++, you have a standalone application or process that runs for perhaps weeks at a time and the data (objects) are persistent between sessions (maybe closing the session doesn't even terminate the process). PHP on the other hand, feels like a new process per page load and the data stored in the objects doesn't seem as persistent.

 

Additionally, PHP is weird in that you have to reload the page to make changes (I'm ignoring AJAX temporarily). That said, something like a chess program makes little sense in PHP as opposed to JavaScript.

 

I'm certain I'm being intensely ignorant of some crucial part of PHP in this post but any insight is appreciated. The internet wasn't very helpful in this regard.

 

Thanks for looking!

Link to comment
https://linustechtips.com/topic/17282-php-classes-and-objects-question/
Share on other sites

Link to post
Share on other sites

I'll use the chess program you mentioned as an example...

 

You're right in assuming that a chess program makes no sense in PHP, but makes sense in JavaScript. The reason you're correct is because the two languages have two different goals in mind. PHP is server-side, completely server-side. Everything you do affects the output the server passes to the client, whereas JavaScript is client-side and affects the presentation of the data on the client side. So, for animations in a chess program, JavaScript would move the pieces on the board without reloading the page (via AJAX) and it would have a callback to the server to adjust the position of the pieces in the database. In the event the chess game had an artificial player, the logic for moving the next piece would be processed server side and passed back to the client where the movement would be handled by JavaScript.

 

Now, continuing to extend this Chess game example, you would create PHP classes to represent the chess board and the pieces. That way, you could instantiate each class and the board on each call from the JavaScript. The database acts as the persistence layer because you'd be querying it to find the location of the pieces on the board every time you made a call to the server.

 


 

In general, though, classes and objects make sense if you're creating a reusable framework or library to handle each individual request. Take frameworks like Laravel, Symfony, Zend, or FuelPHP and you'll see they're all object oriented. Object oriented design helps to assist in the task of reusing pieces and allows you to create hierarchies that allow inheritance and extension of components already in existence. The most common design pattern is the MVC (model, view, controller) where the Model represents the data in the database, the View determines the presentation of said data, and the Controller calls the Model for data and then passes the data to the View. The Controller is supposed to be the least bloated of the three components and it's also the glue that holds the design pattern together. Think of encapsulating your pages into models, views, and controllers and you'll find that you'll be developing a primitive framework you can reuse for many different website projects you may work on.

 

Also, remember when you're querying the database that you shouldn't use the mysql_* functions. Use PDO or MySQLi, they're object oriented (and functional, actually), safer, and not deprecated SQL extensions.

 

I hope this post helps to answer your questions.

Link to post
Share on other sites

Thanks Spartan,

 

Your example extending the chess program was very helpful. I imagine it's my reluctance to learn AJAX that's really hindering my understanding of using objects in PHP. I should really get on that. Thanks for your time and input!

 

As for your last note regarding MySQLi, I didn't know the basic mysql functions were deprecated or shouldn't be used. But as luck would have it, I learned using MySQLi calls to begin with. Yay!

Link to post
Share on other sites

Greetings viewers!

 

So over the past 6 months, I've been playing with PHP (mostly procrastinating and filling the gaps between classes) and I feel fairly comfortable with it. Except that I use it largely functionally and not as an object-oriented language. Although I have a fair understanding of why you'd be inclined to create PHP classes (personally, I'd use them for storing a load of static variables and methods) I'm not really sure under what circumstances I'd create objects.

Personally, I've done one major project in PHP (as in a small company website). I

used OOP mostly because it helped me organize my code better, and that's pretty much

it to be honest. Despite what some people claim, OOP is not a requirement to do

serious coding (remember that most crucial components of today's major Operating Systems

are programmed in C, with some assembly language and a bit of C++ thrown in, so for

the most part they indeed do not use any OOP).

OOP is well suited for some things because it allows (at least some people) to create

a conceptual connection between their code and whatever they are trying to model

(chess, database connections whatever have you). But it's not the only way to do

things.

Bottom line (at least for me): I use OOP in PHP because it allows me to organize my

code pretty well without spending a ton of time refactoring said code to make it all

neat and tidy after the fact (I still do quite a bit of refactoring, but that's just

proper procedure, at least for certain projects).

More background: In Java or C++, you have a standalone application or process that runs for perhaps weeks at a time and the data (objects) are persistent between sessions (maybe closing the session doesn't even terminate the process). PHP on the other hand, feels like a new process per page load and the data stored in the objects doesn't seem as persistent.

You're on the right track. As Spartan has said, PHP is completely server-side. When

a page is requested, the corresponding scripts are executed and their output is then

sent to the client. After that, the process is finished. A new one is then started

when you request the next page.

You can have some persistency between page loads using POST or GET variables as well

as cookie and session variables, so you can indeed preserve data between page loads

(for example, language settings are a prime example of this, you don't want to have

to select your language every time you load the next page). But if you want the server

to do something new, you have to initiate another process by requesting a new page (or

reloading the current one).

Additionally, PHP is weird in that you have to reload the page to make changes (I'm ignoring AJAX temporarily). That said, something like a chess program makes little sense in PHP as opposed to JavaScript.

Well, not necessarily weird, that's pretty much the principle of server-scripting. Same

thing happens with Perl and other server-side scripts (at least those I know of). For

dynamic interaction between client and server (so a proper back-and-forth without having

to load a new the page in its entirety), you do need Javascript or something of similar

functionality.

I hope this helps somewhat, otherwise feel free to ask for clarification. I'm not an

experienced veteran though, so there's certainly stuff I don't know or things I'm

possibly misinformed about. ;)

BUILD LOGS: HELIOS - Latest Update: 2015-SEP-06 ::: ZEUS - BOTW 2013-JUN-28 ::: APOLLO - Complete: 2014-MAY-10
OTHER STUFF: Cable Lacing Tutorial ::: What Is ZFS? ::: mincss Primer ::: LSI RAID Card Flashing Tutorial
FORUM INFO: Community Standards ::: The Moderating Team ::: 10TB+ Storage Showoff Topic

Link to post
Share on other sites

I used OOP mostly because it helped me organize my code better, and that's pretty much it to be honest.

 

Alpenwasser, I really appreciate your honesty. In fact, that's precisely what I've been using the OO nature of PHP for before posting my original question.

 

Spartan, this isn't to say your answer wasn't helpful (in fact, it cleared up many misconceptions I held previously) but it's a matter of understanding the material in order to put it to good use. As of right now, my best understanding is that of using the OO-ness for structured coding.

 

Thanks guys

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×