Page 1 of 1

Need help in best practice with Classes and Methods

Posted: Sat Sep 06, 2014 10:38 am
by kkathman
I have a program set that I'm trying to "retrofit" to better practices. To summarize, I originally used a php include that just consisted of a bunch of functions that did things like create the HTML at the top of a page, retrieved certain data from a database, did assessments of data relationships and returned a set value, etc.

Several of the functions were simple things that did not require parameters and did not return them. For instance the following function to place at the top of each page

Code: Select all

function main_page_header($page_title) {
     echo '<!DOCTYPE HTML>';
     echo '<html>';
     echo '<head>';
     echo '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">';
     echo '<title>' .$page_title. '</title>';
     echo '<link href="css/main.css" rel="stylesheet" type="text/css" />';
     echo '</head>';
     echo '<body>';
   }

I would just include the main_fns.php include on each page, then call this function with like main_page_header("Welcome to the Main Site"); type of statement.

I now want to replace this main_fns.php with a Class that includes various public functions. My biggest problem, though, is how exactly to invoke that same method in my code. Here's a portion of what I have:

Code: Select all

<?php

    require_once ('MySQL.php');

    Class FileInfo {

        public function pageStart($page_title) {
            echo '<!DOCTYPE HTML>';
            echo '<html>';
            echo '<head>';
            echo '<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">';
            echo '<title>' .$page_title. '</title>';
            echo '<link href="css/main.css" rel="stylesheet" type="text/css" />';
            echo '</head>';
            echo '<body>';
        }

        public function pageEnd() {
            echo '</body>';
            echo '</html>';
        }
In my main line of code I simply require_once the php class, then instantiate it with $fi = new FileInfo(); statement.

However when I go to use the function with $fi->pageStart("Welcome to the Main Site"); nothing happens. In fact, the program acts as if it has errors but none are reported.

Can someone point me in the right direction on how to make this work?

Thanks!

Re: Need help in best practice with Classes and Methods

Posted: Sat Sep 06, 2014 12:27 pm
by Celauran
First, you shouldn't be including files all over the place. Namespace your classes, set up autoloading, and only require the autoloader itself. Give your classes and their namespaces meaningful names. The class you've listed above has nothing to do with files, so the naming is confusing.

Next, you've got a require call at the beginning of your class file. In addition to what I mentioned above, this is bad because it creates tight coupling between two classes that have nothing to do with one another. If a class depends on another class, you want to inject the dependency either via the constructor or through a setter method. Ideally, for more flexibility, you'd type hint the interface and inject whichever implementation you need at the time. Fabien Potencier wrote an excellent series on dependency injection.

Now digging into the class itself, let's look at pageStart. It echoes a bunch of output rather than returning it, which is a no-no. Second, you have hard-coded your character set (which should probably be utf-8, but that's another issue altogether) and your stylesheet, and you have not allowed for any additional meta tags or style sheets to be added, which makes things needlessly rigid. At best, this could be a snippet you store in your editor for quick access.

Ultimately, it looks like you're trying to set up some sort of templating system. This is a problem that has been solved many times, and is standard in any MVC framework. If you're looking at implementing better practices and enforcing a better separation of concerns, I'd recommend looking into one of these. Cake 3, Symfony 2, and Laravel 4 are all great, and there's Slim, Silex, and a host of others if you're looking for something more lightweight.

Re: Need help in best practice with Classes and Methods

Posted: Sat Sep 06, 2014 2:15 pm
by kkathman
Celauran --

Many thanks for your input. I've been coding for a long time, but have recently just tried to get back into it after a foray into WordPress and the like. I am not an expert in Classes and Methods, as you can see. I'm trying to understand what's best, and your response, while thorough, raises lots of questions that I need to research.

Templating wasn't my idea. I have an application for a non-profit that just allows folks to register for classes. The application works, but I was trying to use the write once use multiple paradigm. I do put that same "pageStart" code on every page of my functions and as I said, I just referred to it. I kind of got the idea that the class/method way of doing this was wrong for that. I will probably just abandon using the Classes and Methods way and use my require_only set of functions, but the thing is that all those functions aren't need in every function I write.

I have tried MVC frameworks, and had some relatively good luck with CodeIgniter as it was easy to learn. I did a foray into Symfony also, but found that way too hard to deploy and finding others that knew it was few and far between.

CakePHP has been mentioned before and perhaps I'll gander at that again.

Again, thank you for your help!

Re: Need help in best practice with Classes and Methods

Posted: Sat Sep 06, 2014 4:12 pm
by Celauran
Setting up a template and injecting your content isn't a million miles away from what you're trying to accomplish; rather than including your header and footer in each page, you include the content of your page in the template containing the header and footer.

As for MVC, it can be a little hard to get your head around initially. CodeIgniter may have been fine in its day, but that day has long passed. If you're looking to give MVC and frameworks another go, Jeffrey Way has a great introductory series over at Laracasts that you might find helpful.