Where to transition from here?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tmac25
Forum Newbie
Posts: 21
Joined: Fri Dec 28, 2012 5:24 pm

Where to transition from here?

Post by tmac25 »

Hello,

My company allows me the creative freedom to develop anything that could be seen as a benefit to the company as a whole, be it a report generator or a function that gets us out of excel sheets. So, I have been building a project for months now, however going over it, it's mostly (entirely) procedural, but it's like that because everything it does is mostly unique (in my eyes). From my junior experience, I'm trying to see what options I have to continue to develop this project and ensure that it is more scabeable as the project goes on.

My current setup utilizes a Front Controller and View, but that's as far into the MVC pattern that I believe I (can) follow. Essentially, a user will browse to the page, run a report by selecting criteria that are typically specific to that report. It will be based on an "OnClick" attribute that is registered to a function in "ajax.js" which fires an ajax call to "ajax.php" which handles the server-side scripting and the ajax request will update the HTML of a set <DIV> to produce the view for the user.

I've been trying to think about how I can consolidate my code, possibly find a way to have a function for javascript and PHP that I can just pass parameters like the SQL and IDs of inputs used, something. Let alone, I haven't really even gone off to explore JSON results yet.

I'm just looking for general guidance on where I should look to push myself more into OOP and/or consolidating my code, because right now, there is A LOT of copy/page and re-arranging going on.

Thank you for your time!

Examples below, as you can see, imagine ajax.js will pass parameters of inputs and ajax.php will handle all the SQL and HTML presentation. This is the backbone of the majority of my pages, each page will have a minimum of one JS function and 1 php function dedicated to it to ensure data is propagated properly. Ajax.php is a case/switch which is called by the ajax.js url indicating the $_GET.

ajax.js

Code: Select all

function updateStatus() {
    $(function () {
        var toggle = $('#updateVTO').val()
        $.ajax({
            url: 'inc/ajax.php?p=updateStatus',
            type: 'POST',
            data: 'toggle=' + toggle,
            dataType: "html",
            success: function (html) {
                $('#container').html(html);
            }
        });
        return false;
    });
}

ajax.php

Code: Select all

        case 'updateStatus': {
                $toggle = $_POST['toggle'];
                $employeeid = $_SESSION['employeeid'];

                $sql = "SELECT * 
                    FROM dbo.vto_status where status=$toggle";
                $result = sqlsrv_query($msdb, $sql, array(), array('Scrollable' => 'buffered'));
                if ($result) {
                    $rows = sqlsrv_has_rows($result);
                    if ($rows === false) {
                        $sql = "UPDATE ww_wfm.dbo.vto_status
                            SET status = '$toggle'";
                        $result = sqlsrv_query($msdb, $sql);
                        echo "<br />Status successfully updated!";
                    } else {
                        echo "<br />Status is already set to that!";
                    }
                }
            }
            break;
Last edited by tmac25 on Thu May 09, 2013 1:08 pm, edited 1 time in total.
Strider64
Forum Newbie
Posts: 14
Joined: Sun Jun 06, 2010 10:22 am

Re: Where to transition from here.

Post by Strider64 »

I know most people don't like reading books, but I have been reading "PHP Advanced and Object-Oriented Programming (3rd Edition)" by Larry Ullman. The book covers advanced PHP techniques, the theory behind OOP and how to write OOP. This is first book that I have (still am ) started from the beginning and I'm currently in the middle of chapter 8. Since reading this book I been trying to block on the nonsense that most people have been writing on the internet on OOP and I have come to the conclusion that there is nothing wrong doing it the Procedural way if it's a small project or a one time thing. To me using OOP just to use OOP is just silly.
tmac25
Forum Newbie
Posts: 21
Joined: Fri Dec 28, 2012 5:24 pm

Re: Where to transition from here.

Post by tmac25 »

Strider64 wrote:I know most people don't like reading books, but I have been reading "PHP Advanced and Object-Oriented Programming (3rd Edition)" by Larry Ullman. The book covers advanced PHP techniques, the theory behind OOP and how to write OOP. This is first book that I have (still am ) started from the beginning and I'm currently in the middle of chapter 8. Since reading this book I been trying to block on the nonsense that most people have been writing on the internet on OOP and I have come to the conclusion that there is nothing wrong doing it the Procedural way if it's a small project or a one time thing. To me using OOP just to use OOP is just silly.
That's actually what I was thinking! I know why OOP should be used (portability, scaleable, etc.) but the question is when, and from what I have read, a lot (not all!) people on the internet have emphasized moving everything to OOP. But the way I look at it, if you have functions that are all unique, rewriting them all in OOP seems unnecessary.

I see Amazon carries this book, so I'll check it out.

Thank you!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Where to transition from here.

Post by Christopher »

tmac25 wrote:But the way I look at it, if you have functions that are all unique, rewriting them all in OOP seems unnecessary.
Both OOP and Procedural implementations have "functions that are all unique". The difference is they way of thinking about data and code. OOP not only allows scoping that reduces globals and many parameter, but also allows some new design constructs because you pass around code as well as data.

I would recommend Refactoring your code slowly. First you need to understand your Domain and define what all the things are in your application. They you can selectively begin to convert the code without changing functionality. But the order you do things may be important as you restructure you application and all the dependencies.
(#10850)
tmac25
Forum Newbie
Posts: 21
Joined: Fri Dec 28, 2012 5:24 pm

Re: Where to transition from here?

Post by tmac25 »

Thank you both for your replies. Christopher, I apologize, I wasn't trying to imply that OOP didn't have any benefits or that functions unique from each other. What I was getting at is, with my current level of knowledge, refactoring my code doesn't seem to have any added benefit, which I'm sure there is! Which is why I'm looking for guidance because I know there has got to be something I am missing.

That being said, what I'm primarily concerned about is that ajax.php and ajax.js are continually growing at a very rapid pace due to the amount of reports/functions I'm creating. Is there any suggestions/guidance you can think of that I might be able steer toward (not looking for code, just advice on what I can do).

I've been trying to experiment with all-in-one functions like a function that will auto-populate the headers and table data by just sending the SQL as the parameter, ideas like that! This would drastically cut down on the copy pasting I do to create table data and ajax functions.

Let me know what you think!

Thank you!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Where to transition from here?

Post by Christopher »

tmac25 wrote:That being said, what I'm primarily concerned about is that ajax.php and ajax.js are continually growing at a very rapid pace due to the amount of reports/functions I'm creating. Is there any suggestions/guidance you can think of that I might be able steer toward (not looking for code, just advice on what I can do).
It sounds like you are using ajax.php as a Front Controller. If so then loading Action Controllers for the various requests is the common solution. You can easily load classes following PSR0 (even if the Front Controller is not autoloading).
tmac25 wrote:I've been trying to experiment with all-in-one functions like a function that will auto-populate the headers and table data by just sending the SQL as the parameter, ideas like that! This would drastically cut down on the copy pasting I do to create table data and ajax functions.
I would recommend not sending SQL, but instead create a Model object that can fetch the data. This is where polymorphism is a real advance over procedural programming. You can have code that formats table data in the style you like. Give it an object with a fetch() method and the formatter does not have to care what data it is formatting. The interface is standard.
(#10850)
tmac25
Forum Newbie
Posts: 21
Joined: Fri Dec 28, 2012 5:24 pm

Re: Where to transition from here?

Post by tmac25 »

Christopher wrote:
tmac25 wrote:That being said, what I'm primarily concerned about is that ajax.php and ajax.js are continually growing at a very rapid pace due to the amount of reports/functions I'm creating. Is there any suggestions/guidance you can think of that I might be able steer toward (not looking for code, just advice on what I can do).
It sounds like you are using ajax.php as a Front Controller. If so then loading Action Controllers for the various requests is the common solution. You can easily load classes following PSR0 (even if the Front Controller is not autoloading).
tmac25 wrote:I've been trying to experiment with all-in-one functions like a function that will auto-populate the headers and table data by just sending the SQL as the parameter, ideas like that! This would drastically cut down on the copy pasting I do to create table data and ajax functions.
I would recommend not sending SQL, but instead create a Model object that can fetch the data. This is where polymorphism is a real advance over procedural programming. You can have code that formats table data in the style you like. Give it an object with a fetch() method and the formatter does not have to care what data it is formatting. The interface is standard.
Thanks Chris! I appreciate your responses, would you have any recommendations on where to start reading about action controllers? Not really turning anything up on Google.

I'll have to start looking into OOP then, I was not aware it was capable of performing that sophisticated of a function.

Thanks!
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Where to transition from here?

Post by Christopher »

This article (http://phpmaster.com/front-controller-pattern-2/) covers the basic ideas, but in an old-school Java style way. If you search for "php front controller" and you should find information about "dispatch" which is the act of loading and executing a class by the Front Controller.
(#10850)
Post Reply