Solved

PDO Classes

Posted on 2014-09-09
9
112 Views
Last Modified: 2014-09-10
Hi,

I am creating a small cms and am transitioning away from the older style to OOP. OOP is a little cumbersome and all of the tutorials and articles i have read offer conflicting information.

Currently, i am using PDO to access my database so i am using a mixture of OOP and the old style. However, i am not sure how to take it to the next level.

from my understanding, I perceive that creating a class is like defining an intangible object such as a pdf.  Additionally, OOP offers an easier way to view and edit your codes.

From here i am perplexed as im not sure what code i would want to keep together, why or how. Would i create a smaller class and then use extends on it. would i wrap my new PDO code into a larger database code which could perform queries and fetch results in addition to connecting to the database.

I also read that it is wise to keep your viewable code (the code that populates users views) and the executable code (core codes) separate and apart. It is not advisable to include html code in core codes. Once again in a cms most of the code evolves around getting, updating and inserting data. The viewable code is much more common than the so called executable codes. (im not sure if this is a best practice or what).

I have been watching some of the lynda videos but find them confusing as they do not deal with real implementations.

I have also been slowly reading a e-book which one of the members here advised me to read.

Any light on the scenario is welcomed and appreciated

thanks

jayme
0
Comment
Question by:M. Jayme Nagy
  • 4
  • 3
  • 2
9 Comments
 
LVL 58

Assisted Solution

by:Gary
Gary earned 166 total points
ID: 40313427
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40313509
There's an old book about software development called, "The Mythical Man Month."  It has some timeless advice, including a chapter called "Plan to Throw One Away."  The tenet is very simple: The first thing you build from any design will turn out to be useless and unmaintainable.  Hence the advice, "Plan to throw one away."  In reality, the simple fact is that you will throw one away; that is not in dispute.  The only question is whether you will do the necessary planning, or you will deliver the throwaway to your client first, then find that it has to be thrown away.  Clients don't like that.  

So make your plans to build one system in order to learn how to build the system.  Then once you have built one, made the mistakes and learned the lessons, build the second one.  It will be much better -- probably worth keeping.  Just be sure to budget the time you need for this two-step process.
0
 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 40314914
Hi

@Gary. I am assuming that these methods would be wrapped into a larger database class with multiple methods.  Additionally, i would have to include the additional code between the curly brackets of the methods.

ex:

@Ray Not sure if i have time to read another book i am reading one you recommended to me 'PHP and MysQL web Development.' However, i think that planning on certain areas to be replaced and/or updated is essentially common to most things were improvements are regularly occurring. It is one of the reasons why i am jumping up from the old style to OOP. However, i am kind of learning that OOP is highly based on the programmers logic and there seems to be no clear foundation which is confusing as one programmer will address the same problem a little differently than another. If you use there work to compare and learn it becomes difficult to decipher.

are there any tools that i can use to get me started (i dont want to rely to heavily on third party approaches as it does not help me learn very much - i like hands on approach)

thanks guys
0
 
LVL 58

Expert Comment

by:Gary
ID: 40314916
I don't know because I don't really know what your question is.
The link is for a pdo wrapper class so you don't have to worry about building up your statements - you just call the insert, update etc.
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 6

Author Comment

by:M. Jayme Nagy
ID: 40315009
Hi,
Sorry if im not clear im just not very clear on the whole OOP at the moment

would i do something like so?:
class database {
	
	//DEFINE CONNECTION VARS
	private $host;
	private $user;
	private $pass;
	private $name;
	
	//CONSTRUCTOR METHOD
	public function __construct(){
		
		//INITATE PDO OBJECT
		// OPEN A CONNECTION TO THE DATA BASE SERVER AND SELECT THE DB
		$dsn = "mysql:host=".$this->$host.";dbname=".$this->$name;
		
		//SETUP THE OPTIONS
		$options =array(
			PDO::ATTR_PERSISTENT 		=> true,
			PDO::ATTR_ERRMODE	 		=> PDO::ERRMODE_EXCEPTION,
			PDO::ATTR_DEFAULT_FETCH_MODE=> PDO::FETCH_ASSOC
		);
		
		try {
			$db = new database($dsn, $this->$user, $this->$pass, $options);
		
		} catch (PDOException $e) {
			
			die( 'Connection failed!');
		}
		
	}
	
	
}

Open in new window


Another question is: would i want to make the connection vars static
ex:
class database {
	
	//DEFINE CONNECTION VARS
	private $host ='host';
	private $user = 'user';
	private $pass ='pass';
	private $name= 'name';

      ........


}

Open in new window


if i do not make them static than the class become more flexible but then i would have to add another methods to get/set the vars. Is this a correct approach for connecting to a database?
0
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 334 total points
ID: 40315086
OOP is highly based on the programmers logic and there seems to be no clear foundation
Erm? Actually there are very well-defined principles.  And if you don't want to read "the book" you'll have to go with trial-and-error.  I guarantee that takes longer than structured learning, and the result is much less certain.

This is "the book."
http://www.amazon.com/Design-Patterns-Elements-Reusable-Object-Oriented/dp/0201633612

You need this one, too.
http://www.amazon.com/Patterns-Enterprise-Application-Architecture-Martin/dp/0321127420/

And specific to PHP.
http://www.amazon.com/Objects-Patterns-Practice-Experts-Source/product-reviews/143022925X/
0
 
LVL 108

Accepted Solution

by:
Ray Paseur earned 334 total points
ID: 40315100
if i do not make them static than the class become more flexible but then i would have to add another methods to get/set the vars. Is this a correct approach for connecting to a database?
"Static" is a term of art.  I think you mean "predefined" instead of static.  Since all your constructor is doing seems to be a wrapper for the PDO constructor, why not just pass the four variables to your constructor then re-use them in the PDO call?

I don't think I see anything inherently wrong with the connection code posted above, but it does not make sense to catch an exception when all you're going to do is die().  It might be useful to see what is contained in the PDO Exception object.  You might be able to generate a useful error message, or retry the connection if the failure is recoverable.

If I were just starting out in OOP, I think I would have a look at gr8gonzo's articles here at EE.  He knows what he's doing.
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
0
 
LVL 6

Author Closing Comment

by:M. Jayme Nagy
ID: 40315117
Looks like i have a heavy load of reading ahead

thanks
0
 
LVL 108

Expert Comment

by:Ray Paseur
ID: 40315135
Yes, you do.  Don't underestimate the time it can take to learn this stuff.  But that up-front investment pays huge dividends when you start using the principles in your work.  Along the way you might want to develop a personal library of code examples.  Some of them will be good and reusable, and all of them will contribute to your knowledge base.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now