Page 1 of 1

Brand Spankin' New to OOP

Posted: Sat Jan 19, 2008 1:04 am
by jeffrydell
I'm trying to convert an app written in the old linear fashion (the way I can think and write FLUENTLY) to OOP.

While I know I have to change all my $_SESSION references to fit the OOP syntax, I'm not sure if I can still do includes the same way.

It would be a BIG help to me if I could still keep some of the 'static' stuff in separate files and include them in my functions - er - methods. Will that still work?

Thanks for your patience ... like I said, I'm really new to this.

Re: Brand Spankin' New to OOP

Posted: Sat Jan 19, 2008 1:38 am
by Christopher
Post some example code of the first thing you want to convert and people can give you some ideas.

Re: Brand Spankin' New to OOP

Posted: Sat Jan 19, 2008 10:45 am
by jeffrydell
Wow - you guys are hospitable! Not quite the type of response I may have expected from other forums (some can be pretty rough).

I'm working with the ZervWizard class which handles multi-page form navigation. Each form is called a 'step', each step has a prepare_stepname() and a process_stepname() which populates default values and validates POST data for one of the forms in the interview.

Some of the prepare scripts can get rather involved because of a lot of conditional situations, and some of the forms have a LOT of fields, so I'd prefer to keep those scripts 'outside' my class script which extends ZervWizard, just for ease of viewing like this for the first step:

Code: Select all

function prepare_access()
{include('accessprep.php')}
 
function process_access()
{include('accessproc.php')}
 
I know, it's confusing saying verb first / noun second in the method name and then reversing it for the filename, but it makes the files much easier to find (the names are together in the directory and they don't all start with 'p').

Then, in the accessprep.php file:

Code: Select all

 
<?php
if (!isset($_GET['id'])
{die('Not enough info to proceed')}
else{
$res = mysql_query("SELECT * FROM `events` WHERE `club_id` = " . $_GET['id'])
... all the rest of the script ...
}
?>
Sorry, I couldn't figure out how to indent on this board so the code is a bit tough to read (at least for me).

So, is that a legitimate way to 'include'?

Re: Brand Spankin' New to OOP

Posted: Sat Jan 19, 2008 11:15 am
by alex.barylski
I know, it's confusing saying verb first / noun second in the method name and then reversing it for the filename, but it makes the files much easier to find (the names are together in the directory and they don't all start with 'p').
I'd personally follow a convention...naming the file after the class name or visa versa...I just find that makes maintenance easier in long run.
So, is that a legitimate way to 'include'?
I wouldn't...although I'm not sure exactly whats going on here...

If the wizard class has several methods:

Code: Select all

class MyWizard extends ZervWizard{
  function process_access()
  {
    include 'accessprep.php';
  }
}
Your includes now have a non-explicit contract with any variables defined/declared inside the process_access() method. Technicall it's fine, of course. But code explicitness is lost in favour of code clarity in the wizard class. Personally I would call a function rather than an include, just to make that contract explicit...

Code: Select all

class MyWizard extends ZervWizard{
  function process_access()
  {
    $some_var = $_POST['some_var'];
    accessprep($some_var);
  }
}

Re: Brand Spankin' New to OOP

Posted: Sat Jan 19, 2008 11:28 am
by jeffrydell
Technicall it's fine, of course. But code explicitness is lost in favour of code clarity in the wizard class. Personally I would call a function rather than an include, just to make that contract explicit...

Code: Select all

class MyWizard extends ZervWizard{
  function process_access()
  {
    $some_var = $_POST['some_var'];
    accessprep($some_var);
  }
}
[/quote]

I follow what you are saying (I think - it's still very early in the game for me to be confident about much of anything ... at least until I get something to work right). Much of what I am doing is what it is because I just want to try and apply the existing class to my situation and see how it works. By no means do I feel ready to 'reinvent the wheel' when it comes to modifying ZervWizard.

Thanks for the feedback ... I'm sure I'll be back for more!

Re: Brand Spankin' New to OOP

Posted: Sat Jan 19, 2008 11:44 am
by Christopher
I get the impression that you are trying to walk, no sprint, before you can crawl. ;) You're "Brand Spankin' New to OOP" yet you are trying to implement ZervWizard (I read that article too...) which is an implementation of the very complex Application Controller pattern. And on top of that you have functions that are so big that you need to include their contents from a file to make things understandable. Even you noticed that that is a little weird because you asked if people thought is was ok.

I am just trying to figure out what the actual problem is. You are like a guy who goes to a ship building forum, says he is new to building ships, says he is building an ocean liner, shows a picture of a giant piston, and asks if that looks ok. ;)

Includes can be used almost anywhere. Using them to move code out of a file when it is not always executed can be a reasonable choice. In you case you are working with an existing library and it sounds like it is forcing you to organize you code in a way that is not familiar to you -- so naturally you trying to arrange it in files that follow a naming scheme you prefer. That makes sense given that you are both learning the ZervWizard system and need to maintain the code.

Re: Brand Spankin' New to OOP

Posted: Sat Jan 19, 2008 12:35 pm
by jeffrydell
Yep - you're exactly right. I'm trying to learn syntax and visibility and terminology all at once so I can do a major re-write of an application that sorely needs a better navigation structure. I haven't been able to find a good solution to the navigation management issues in the 'old php world' with linear scripts, etc. ... so I'm trying to jump in to OOP and get my application up to snuff PRONTO.

I'm doing this project for myself, there is a void in the market which needs to be filled, I'm already 'sort of out there' with my app, but it needs to be cleaned up before I can serve the full market ... so I'm scrambling.

After experimenting with this more, I have a better sense of what works & doesn't work and your comment about using functions seems to be the ticket. If I keep the 'prep scripts' and 'processing scripts' in functions, then all will be good. Learning about how to keep some of the code in separate files for the sake of being easier to find and comprehend is a lesser concern for some time in the future.

Re: Brand Spankin' New to OOP

Posted: Sat Jan 19, 2008 2:48 pm
by Christopher
You code will probably be a hybrid for quite a while as you come up to speed with OOP. Keeping things that are familiar to you around awhile (like naming conventions) can make the transition easier.

The specific problem you are encountering with ZervWizard is due to its design. To be a real Application Controller it should really be able to dispatch Actions -- which would each be a separate class in its own file. Having each "step" be a method is fine for small things, but it is not very modular. You might be able to modify these two lines in that class to load external classes for you:

Code: Select all

                       // processing callback must exist and validate to proceed
                        $callback = 'process_' . $action;
                        $complete = method_exists($this, $callback) && $this->$callback($form);
 

Re: Brand Spankin' New to OOP

Posted: Sat Jan 19, 2008 3:36 pm
by jeffrydell
THANK YOU!

That could end up being a very valuable mod as I face one critical issue - loopbacks.

If I set this up 'literally' to mirror my current app, at one point I'd have a step which would present the option of continuing on to the next step, or going back 5 Steps to add another registration. That didn't seem like a good solution the way ZervWizard is currently set up.

Another option would have been (my preferred approach) to have one step instantiate another 5-step class which would simply be repeated until all registrations were complete, then return to my main class and finish out the remaining 4 steps of the original.

It looks like your suggestion (in addtion to other benefits) will help me accomplish what I need to do.

By the way, I have achieved a 'conditional' "Hello World!". If events are available for registration, it gives you the rundown of the registration process ... if there are no events, it says so ... and if there are no events, but will be some events in the future - it invites the site visitor to return when registrations open up.

Nothing too fancy, but certainly a start. I'm not feeling so guilty about typing $this-> instead of $_SESSION now.

Re: Brand Spankin' New to OOP

Posted: Sat Jan 19, 2008 4:19 pm
by Christopher
Sounds like you are getting there. Post your code and questions as you get them -- and once you have worked through the problem, let us know what you learned about wizards.