PDO Classes

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
LVL 6
J NUnicorn wranglerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

GaryCommented:
0
Ray PaseurCommented:
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
J NUnicorn wranglerAuthor Commented:
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
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

GaryCommented:
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
J NUnicorn wranglerAuthor Commented:
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
Ray PaseurCommented:
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
Ray PaseurCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
J NUnicorn wranglerAuthor Commented:
Looks like i have a heavy load of reading ahead

thanks
0
Ray PaseurCommented:
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.