Link to home
Start Free TrialLog in
Avatar of J N
J N

asked on

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
SOLUTION
Avatar of Gary
Gary
Flag of Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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.
Avatar of J N
J N

ASKER

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
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.
Avatar of J N

ASKER

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?
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of J N

ASKER

Looks like i have a heavy load of reading ahead

thanks
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.