Jump to content

General *good* coding practices for larger projects


greatkingrat

Recommended Posts

So a friend and I have started work on a larger site.  I'm doing the back-end php and mySQL part, and he's designing the pages with HTML / CSS / JavaScript

 

quick background:

I hadn't done much OOP php in the past (and am a novice in php), but decided that this was a good time to start as "Users" have a number of functions, and I also made a class called "Page" which handles all the display functions and such.  It works well, and loads both the classes when any given page is loaded (users pulling from session data or wherever, and "page" based on the page they're on and the page number in the appended URL).  They are semi-dynamic pages. Some stuff will be done with AJAX, but others not so much as AJAX starts another instance on a php script (since it's a different script), so the class variable wouldn't exist on it (I assume).

 

the question:

In relation to "standard" coding practices, is it ok to have my friend's JavaScript output a php string that executes a function?  Like AJAX but JS doesn't get the response (just puts it in .innerHTML of some tag) and it's not calling a different script, just "adding" to the current page.  Passing a variable(s) if nessisary.  EG:

document.whatever.innerHTML = "<?php echo $SomeFunction(); ?>";

 

Building on that, is it ok to basically have a page that is some includes at the top (the classes and various other things I need) and then just wherever php is needed to fill in some data from the database, have a <?php echo $SomeVariable; ?> line (so the page has short spots all over where php fills something in).

 

Other smaller projects I've done just run all the HTML for the page off php echos, but that seems....wasteful and pointless for larger more complicated sites, esp when more than one person is developing it.  I tested outputting PHP with JS and it seems to work fine, but if it's not "good" practice then I don't want to do it.  Same goes for the static PHP bits here and there.  Any feedback and advice on good practices in situations such as this would be helpful.  Thank you.

 

 

tl;dr

short <?php code ?> tags all over an HTML page, some dynamically generated by JS - good idea or terrible idea (or is that the norm for sites). 

Link to comment
Share on other sites

It really comes down to what you are coding for, There is no 'norm' for most sites, I've seen many do it differently and in their own way, as for your JS passing the variables via a function may be more taxing than accessing the class or global directly. On their side, why are you using "document.whatever.innerHTML = ..." ? Why not be able to write directly to 'whatever' as a value?

 

As for the 'wasteful' amount of PHP that is being used, you should encorporate a something such as while loops to display the data, but again, not sure what you're wanting to do.

Link to comment
Share on other sites

IMO

 

document.whatever.innerHTML = "<?php echo $SomeFunction(); ?>";

 

Is a bad idea as is:

 

<?php echo $SomeVariable; ?>

 

throughout your website. IMO that is the worst thing you can do.

 

What I believe would be a good approach is thinking in functionalities for example:

 

I'm building a forum so I may represent my forum through an object (Forum) A user can add topics to a forum

 

class Forum {
     public function addTopic(ForumTopic $t) {}
}

 

An administrator may lock a forum topic

 

class ForumTopic {
     public function lock() {}
}

 

Or move a topic to another forum

 

class ForumTopic {
    public function move(Forum $f) {}
}

 

I want to be able to easily loop over a forum to display it

 

class Forum implements Iterator

 

The same applies for a forum topic to display all posts made to the topic including the OP's post

 

class ForumTopic implements Iterator

 

I usually design this in an UML class diagram afterwards I'd apply design patterns where they apply and implement the diagram

Link to comment
Share on other sites

ignace, I should have been more specific.  The bits of code that you quotes (a function call or variable echo) are actually classes.  So the nature of the website is very much like a forum, and you can make a post of sorts.  So right now, the tables are set up to hold the data and that variables are actually class functions that echo data already pulled from the SQL database (very very close to the examples you sited).  This is true for both classes.  So the page class has functions to output various parts of the page, read the page and set all the data when teh page is loaded, go to the next page, etc etc.  The user class can do things like signup, post, change profile info, everything you do here.  Eventually I'll add an admin subclass to that very much like what you have, but i'm a ways from that right now :)

 

That being said, that answered my question perfectly.  oni-kun mentioned it's not bad practice, and you got even more specific and suggested WHAT should be in those outputs and functions (which happened to be what I was doing).  As I was not looking forward to redoing everything, I am very happy to hear that haha.  The implements Iterator was something I hadn't thought of either and will need to put in.

 

Indecently, the only time we'd use innerHTML (could be document.write, really any output depending) is if it's a dynamic part of the page that won't be seen unless a javaScript even is triggered (say a box that pops up to log in or something).  all the rest are statically written into the page.

 

So that I'm sure I understand you correctly  ignace, if I already have the classes like that, then it would therefore be OK to output (using more accurate versions of my previous examples):

document.whatever.innerHTML = "<?php $page->DisplayPostTitle($row); ?>";  //(actually would be three parts i guess, since JavaScript as to put in the row, close enough)

<?php $user->displayUsername(); ?> //ok, these examples arn't much better, most of the functions do more than just write a variable

 

Finally, most of these I wrote functions to output the variable in the class ($row for instance is the position in an array that was pulled from mySQL).  Siting your examples, this is a better way of doing it than actually echoing a public variable?  Most of what's being called has other things it needs to do as well (a meaningful function unlike my examples) like logging in , moving to the next page, editing a post possibly...etc. 

 

Sorry for the confusion.  Your examples are great though, this isn't a forum but its user generated content very much like a forum.

 

 

Link to comment
Share on other sites

I'm not sure we are on the same page especially when I look at:

 

document.whatever.innerHTML = "<?php $page->DisplayPostTitle($row); ?>";

 

If my assumption is correct that $row is actually a row fetched from a database and that is wrong as it would need to be:

 

document.whatever.innerHTML = "<?php $page->getTitle(); ?>";

 

$page then actually represents $row as an ActiveRecord or some other pattern. Plus like I said IMO something like:

 

document.whatever.innerHTML = "<?php $page->getTitle(); ?>";

 

is wrong and you should in fact make an ajax call to retrieve the required information from PHP.

 

var page = ajax.get('/a-slug-page-title');
alert(page.slug); // a-slug-page-title

Link to comment
Share on other sites

document.whatever.innerHTML = "<?php $page->getTitle(); ?>";

 

is wrong and you should in fact make an ajax call to retrieve the required information from PHP.

 

 

Your right, we are on the wrong page, but this is getting me closer.  My ignorance is making my situation hard to explain! lol

 

So if I have a class that is declared when the page loads, calling a seperate AJAX script wouldn't work (I started building it with AJAX then realized it was a perfect opertunity to start using classes and such, as I'd never had a reason to before).  So say I declare the page class when the page loads to page 1.  It pulls data that it needs from mySQL, etc etc.  Then theres an HTML table for each blurb about hte post (a post title, username, and short description).  each of those three parts is pulled , in this example, with static PHP tags

 

...html table etc....
<td><?php $currentPage->displayTitle(1); ?></td>
...html table etc....

where 1 is the row (static table so the next one passes 2, etc etc).  Since it's static, it's hardcoded.  That's the easy part.  If this is poor practice, then let me know and stop reading here haha.

 

Building on that, I would therefore be able to OUTPUT that same line on a JavaScript event as well (outputting the above code in innerHTML or any other JS method of writing).  Again, this basically is acting like AJAX but it's executing in the same PHP file, so I can access my defined page and user class variables. 

THIS is where i'm confused.  Can that even be done since it appears that the PHP is actually executed IN THE JAVA SCRIPT on the PAGE LOAD, which means if anything changes in the database in the mean time, it won't actually output the new data.

 

eg. i output php code that is a function to grab the value of "foo" from the db and echo it.

Page loads, java script is read in by the browser, and it grabs foo (with it's current value) since the php executes

user clicks a button that CHANGES foo in the database

user clicks a button to display foo

 

the OLD value (from page load) would be output since the php in the JS function executes on page load (would a work around be breaking it up in the JS output with "+" ?)

 

So I guess in the end that's my real question => If I have classes that define themselves when the page loads and are used for almost every function, then how can I call dynamic PHP code based on events that happen (clicks, mouse overs, etc) like AJAX would but still be able to use those already defined classes

 

This setup would be really easy if I could call php via AJAX, but have those other php files already have the same class I defined on this main page (for both Page and User, both classes will run in to this problem).  Maybe I'm coming at the OOP approach wrong here....eg classes:

page

    function to read in sql information for the table into a variable used when various print functions are called

    function printTitle($row)

    function nextPage()

    ...

 

users

    login

    loadFromSession

    loadFromCookies

    VerifyPass (private, called in login)

    signup

    vote

    post

...etc

....and some variables

 

 

 

Link to comment
Share on other sites

Marking as solved.  I did some further reading and found out the following:

The "model" of site I'm trying to build is called MVC apparently.  This project we are then using the framework for to build another site, which coupled with the fact that my friend is doing the frontend and I the back-end, MVC was exactly what I was going for.

 

During those searches I also researched some frameworks.  This made me realize that when building using a MVC model, my classes are more like the Database tables, so a "page" class shouldn't even exist (though users should).  I was close, but a little off on how to build in OOP php.  I build the classes which will just have a few modifications for the next site (based on the nature of the content, new rows in the tables) and I also build the the interaction with this (basically, I'm building the Model and Controller, he's building the View). 

 

I debated using Yii, but ultimately we're BUILDING the framework, so although it may work easier, I'd like to try it myself since our secondary goals for this site is to further our own abilities as developers. 

 

Finally, a combination of static classes through the Singleton method, and serialized class saved to the session (only for user, I imagine this is taxing on memory and it's not needed for other classes) solves further issues regarding some of the pages that should ultimately be using AJAX (and other places, just php code) that needed access to an already defined class and/or variables. 

 

I'm sure Ill have more spesific questions in a few weeks when I get in over my head with this stuff.  Thank you for the help. 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.