• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 163
  • Last Modified:

How would I do this using an OOP approach?

I'm resolved that I'm going to learn OOP. I've started documenting my "journey" at http://brucegust.com/campaign/OOP/ and you can check that out, just so you can see my thought processes so far, but here's what I want to do.

I want to take a project that I would normally code using a Procedural approach and do it using OOP. I'm having  tough time finding the specific tutorials that would show me exactly what I need to do, though I know what needs to be done. Hence, here am I once again, a peasant among kings, asking for some direction.

I've got a table with data that's coming from a select statement that I'm looping through. Using a procedural approach, here's what I'm looking at:

<?php
$querystate = "SELECT * FROM registration order by registration_type";
$querystate_query="mysqli_query($cxn, $querystate)
or die("QueryState didn't quite make it!");
$querystate_count=mysqli_num_rows($querystate_query);
if($querystate_query>0)
{
while($querystate_row=mysqli_fetch_assoc($querystate_query))
{
extract($querystate_row);
?>
<tr>
<td>
<?php echo stripslashes($querystate_row['first_name']); ?>
</td>
</tr>
<?php
}
?>

Now, using an OOP approach, I'm thinking the syntax looks different in that I'm going to be referencing $querystate as sql in the context of function within a class. I'm going to instantiate an object that references that class and I'm going to be using the $this-> configuration to grab my "first_name."

How?
0
brucegust
Asked:
brucegust
  • 4
  • 2
  • 2
2 Solutions
 
gr8gonzoConsultantCommented:
I'm also not quite sure I understand your end goal. Are you just trying to learn the OOP way of using MySQLi? Or are you trying to create your own class that makes use of MySQLi?
0
 
Ray PaseurCommented:
There are entire books written about OOP, and PHP even has a few that are specific to the language.  You probably want to get this one.  It's not exactly light reading, but you have to understand this stuff if you want to use object-oriented design techniques
http://www.amazon.com/Objects-Patterns-Practice-Experts-Source/dp/143022925X/

Object-oriented code and object-oriented design are two separate things.  They play well together, and you can write procedural code that uses object-oriented notation.  You cannot create object-oriented designs with procedural code.

Not every programming task needs or benefits from OOP, and this small example won't pick up much benefit at all.  Larger projects, however, will get a lot of benefit.  To get a feel for what OOP can do for you, consider this real-life scenario with one of my clients in Maryland.  The assignment was simple - or so they thought.  They are a trade membership organization with two classes of membership, regular and premium.  And they wanted to add one more class of membership, "super."  There is only one data base table that carries the class of membership.  But here is where the rub comes in.  Their entire web site (hundreds of pages) is built in procedural code!  I put a code scanner on the site and found 408 references to the membership class column, and all of them carried an assumption about the contents of the column.  It was assumed to be either "r" or "p" -- there was no provision for a third class of membership.  The code was inconsistently implemented and said things like "if not 'r' assume 'p', etc."  There was no rule of logic -- every code point was slightly different.  

In order to add the new "super" class, you have to make and test 408 programming changes.  If you have to take 30 minutes to rewrite and test each code point, you're looking at more than 5 weeks of work!

Now imagine if this had been done in an OOP design.  There would be a member object.  The member object would have a method to return the membership class.  The class constructor would create everything you needed to know about the member at the time an object instance of the member class was generated.  To add the new membership class, you would have one programming change and you would know exactly where to make it and how to test it.   The job could be done in an afternoon!

E-E has some other learning resources that will be helpful to you.  These from @gr8gonzo are good.
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_2626-Beginning-Object-Oriented-Programming-in-PHP.html
http://www.experts-exchange.com/Programming/Languages/Scripting/PHP/A_2631-Advanced-Object-Oriented-Programming-in-PHP.html

This is untested code, but it shows one way you might go about the process.

<?php
error_reporting(E_ALL);

/**
 * ASSUMES A CONNECTION TO THE DATABASE SERVER IN $this->cxn
 * WHICH PROBABLY COMES FROM A CLASS THAT EXTENDS THIS ONE
 */
Class Registration
{
    public $first_name
    public function __Construct($id)
    {
        if (!$this->cxn) trigger_error("DATABASE NOT CONNECTED", E_USER_ERROR);
        $id  = mysqli_real_escape_string($this->cxn, $id);
        $sql = "SELECT * FROM registration WHERE id = '$id' ORDER registration_type LIMIT 1";
        $res = mysqli_query($this->cxn, $sql);
        if (!$res) trigger_error("FAIL: $sql " . mysqli_error(), E_USER_ERROR);
        
        // RETRIEVE THE ROW INTO AN OBJECT PROPERTY OR RETURN FALSE IF THERE IS NO DATA
        $this->row = mysqli_fetch_object($res);
        if (!$this->row) return FALSE;
        
        // SECURE THE NAME (AND OTHER PROPERTIES AS NEEDED)
        $this->first_name = $row->first_name;
        return TRUE;
    }
}


// EXAMPLE USE CASE: GET THE REGISTRATION FOR USER #123
$obj = new Registration(123);

// CREATE AN HTML DOCUMENT WITH THE FIRST NAME EMBEDDED
$htm = <<<EOD
<tr>
<td>
$obj->first_name
</td>
</tr>
EOD;

// WRITE THE HTML TO THE BROWSER
echo $htm;

Open in new window

0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
brucegustPHP DeveloperAuthor Commented:
Trying to learn oop and I'm just using what I've done with mysqli as a template. I learn best by doing a project rather than going through examples that may or may not have any relevance to what I'm doing. Make sense? So, if I understand your question, No, I'm not trying to create a separate class, rather I'm trying to get my brain around how I would write that SQL as part if a class or method.
0
 
Ray PaseurCommented:
You don't really think in terms of "how would I write that SQL" in OO design.  You think more in terms of "What constitutes an object?" and "What is the interface between objects?"  A classic example I've used in my teaching has been a (hypersimplified) conference schedule web site.  Objects include:

Presentations
Speakers
Rooms

By way of example, there are relationships between Presentations and Speakers but these are separate objects.  Speakers have properties that might include names, addresses, phone numbers, etc.,  Presentations have properties that include duration, audience, etc.  Rooms have identities and locations.  A well-designed site would treat these objects independently, while allowing (probably via DB junction tables) relationships to exist.

So at a basic level, you can think of a row of a data base table as being the properties of an object representing one entity in the data base.

Some of this will become clearer when you become familiar with "design patterns."  Another book!
http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612/
0
 
Ray PaseurCommented:
One more learning resource that will be helpful:
http://www.phptherightway.com/
0
 
brucegustPHP DeveloperAuthor Commented:
Thank you, gentlemen!
0
 
Ray PaseurCommented:
Good luck with it, and if you want a compass for the journey, follow the work and writings of Brandon Savage.
http://www.brandonsavage.net/
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

  • 4
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now