Dev Newb
Dev Newb

Reputation: 565

PHP OOP with Javascript

I am trying to wrap my head around PHP OOP and after reading everything I can I understand fully how it works but before I invest too much time in learning to change my inline, procedural habits I wanted to be sure it could even accomplish what I need or if I am way off track.

I have a PHP / MySQL site that started as a side project but is growing quickly and I am trying to ensure that my skills can keep up. The site has users and companies. The users side is fairly static and I'm able to handle minor changes in what is displayed with my newbie PHP skills. On the company side I tried to make it very customizable for the companies which they love but I quickly realized I had a problem because now I have 50 pages of code per company that was being modified and then dumped into folders based on what the company wanted on their site. So everytime a company came on board I copied the previous companies files into a new file folder in my web root and customized a bunch of stuff for them. I've put some functions into most of the pages to narrow the number of pages down to about 20 but my question is can OOP bring me all the way home to where I am just maintaining one site and is that the purpose of OOP? I have things like javascript (shown below) that changes based on what the customer wants in their grid system. Can someone help me wrap my head around the basics of what I should be learning to do here? Should I be learning to make each page one big object and include the javascript below in that object or is the OOP just for PHP and I am stuck with at least a few pages of code ending up in a new folder everytime a customer comes on or do I just limit what can be customized? I am not afraid to learn something new, just want to be sure I'm on the right track and the hole I'm in is getting deeper quickly so time is short. Thanks for any help with this concept. I feel there is some basic understanding that I'm missing...

var mygrid;         
            function doInitGrid(){  
            mygrid = new dhtmlXGridObject('gridbox');
            var combo=mygrid.getCombo(4);
            combo.put(2, 'Approve / Deny');
            combo.put(1, 'Approve');
            combo.put(0, 'Deny');
            mygrid.setImagePath("../codebase/imgs/");           
            mygrid.setHeader("Submit Date, Manufacturer, Product Category, Progress, Approval Status",null,["text-align:center;","text-align:center;","text-align:center","text-align:center","text-align:center"]);        
            <!--mygrid.attachHeader("#text_filter,#text_filter,#text_filter");-->
            mygrid.setInitWidthsP("10,16,44,18,12");
            mygrid.setColAlign("center,left,left,center,center");
            mygrid.enableAutoWidth(false);
            mygrid.setColTypes("ro,ro,ro,button,coro");
            mygrid.setColSorting("str,str,str,str,str,str");
            mygrid.enableRowsHover(true,'grid_hover');              
            mygrid.setSkin("modern");
            mygrid.init();          
            mygrid.setStyle("font-size:11px;","font-size:11px;","font-size:11px","font-size:11px","font-size:11px");
            mygrid.loadXML("connector.php?hospid=<?php echo $hospid; ?>");
            mygrid.setColumnIds("date,man_name,group,approval_progress, approval_status");          
            var dp = new dataProcessor ("connector.php?hospid=<?php echo $hospid; ?>");     
            dp.init(mygrid);                        
            dhxWins = new dhtmlXWindows();
            dhxWins.setImagePath("../codebase/imgs/");          
            mygrid.attachEvent("onRowSelect", function(id,ind){ 
                 if (ind == 0 || ind == 1 || ind == 2 || ind == 3){  
                dhxWins = new dhtmlXWindows();
                var w1 = dhxWins.createWindow("w1", 60, 60,925, 575);
                dhxWins.setSkin("dhx_web");
                dhxWins.setImagePath("../codebase/imgs/");
                w1.centerOnScreen();
                w1.setText("");
                w1.setModal(false);
                theVar11 = (id);
                w1.attachURL("pop_details.php?var1=" +theVar11);
                return true;
                 };
                 });

Upvotes: 1

Views: 1136

Answers (2)

Konerak
Konerak

Reputation: 39773

I tried reading your question (ahhh, walls of text! No blank lines, no bullet points, no summary!) and from what I understand, Your biggest problem does not seem to be PHP and OOP, but project management.

You are trying to manage a project for 50 companies, and they all need a few different things, and you're having problem with code-reuse / copy-pasting code together with updating all code.

The solution could be two-part: for the first part, most people would advise you to use a Sourcecode Versioning System, like git, svn or cvs. This will allow you to version your code, create branches, and merge them. Updating old code is a lot easier this way.

The second solution for your code reuse is indeed OOP. You need to decide which functionality is common and should be in a separate (reusable) module, and which functionality is specific to the company and will not be reused. Once it gets reused, separate the code into a module.

It might help to think that you're not writing the code for yourself or for the company, but rather for a team of other developers who will have to use your code. Seperate the code and document the API.

Upvotes: 3

Eli
Eli

Reputation: 4359

Hmmm

I would say that OOP can be very handy here!

First of all, you would want to make all your HTML pages separate from your PHP pages. Reduce the clutter for yourself.

Second, i your PHP create a parent class that has major profile functionality. This will work for you later down the road because you can pass in a few parameters to your class that will update all the relevant information for your user profiles.

When working with a large site like the one yours seems to leaning to, it will make your job as a developer easier to either follow an MVC system or to create your own system.

As an example to your case, create a file called Profile.class.php

class Profile
{
   public function __construct() {}

   public function updateUser($user)
   {
       // run code to update 1 user here
   }
}

in your profile.php page

you can have something like

$obj = new Profile();

if ($_GET['update']) {
   $obj->updateUser($_SESSION['user']);
}

thats how you would be able to benefit from an OOP approach.

The choice is yours.

Upvotes: 2

Related Questions