PDO and OOP PHP

​I think any application has two very important features: Saving/Adding Data to Database and then fetching retrieving it back.​

Rest everything such as front end: HTML and DOM is another detail, including structuring application by using correct and optimized coding practices such as MVC model establishing files and folder + Object Oriented PHP.

Back to the discussion: I was trying to understand PDO I stumbled upon this online free video course → = Section 5 Dealing with the PDO and the Databa

I have shared a link because in the future if anyone comes at this link he may get the reference.

Before writing this post and seeking help I tried to understand as much as I can on my own.

Reference #2

I am copy-pasting the code here of a class name:

Database.php →

<?php 
class Database{
	private $host   = 'localhost';
	private $user   = 'toolcula_apps';
	private $pass   = 'XXXXXXXX##';
	private $dbname = 'toolcula_apps';

	private $dbh;
	private $error;
	private $stmt;

	public function __construct(){
		// Set DSN
		$dsn = 'mysql:host='. $this->host . ';dbname='. $this->dbname;
		// Set Options
		$options = array(
			PDO::ATTR_PERSISTENT		=> true,
			PDO::ATTR_ERRMODE		    => PDO::ERRMODE_EXCEPTION
		);
		// Create new PDO
		try {
			$this->dbh = new PDO($dsn, $this->user, $this->pass, $options);
		} catch(PDOEception $e){
			$this->error = $e->getMessage();
		}
	}

	public function query($query){
		$this->stmt = $this->dbh->prepare($query);
	}

	public function bind($param, $value, $type = null){
		if(is_null($type)){
			switch(true){
				case is_int($value):
					$type = PDO::PARAM_INT;
					break;
				case is_bool($value):
					$type = PDO::PARAM_BOOL;
					break;
				case is_null($value):
					$type = PDO::PARAM_NULL;
					break;
					default:
					$type = PDO::PARAM_STR;
			}
		}
		$this->stmt->bindValue($param, $value, $type);
	}

	public function execute(){
		return $this->stmt->execute();
	}

	public function resultset(){
		$this->execute();
		return $this->stmt->fetchAll(PDO::FETCH_ASSOC);
	}
}

There is a very huge information given out here →
https://www.php.net/manual/en/book.pdo.php

I have lots of questions I am asking one by one here.

Question #1
https://www.php.net/manual/en/pdostatement.bindparam.php

And the code in question →

public function bind($param, $value, $type = null){
if(is_null($type)){
	switch(true){
		case is_int($value):
			$type = PDO::PARAM_INT;
			break;
		case is_bool($value):
			$type = PDO::PARAM_BOOL;
			break;
		case is_null($value):
			$type = PDO::PARAM_NULL;
			break;
			default:
			$type = PDO::PARAM_STR;
	}
}
$this->stmt->bindValue($param, $value, $type);
}

Although, I understand the switch statement, but could not understand what is going on here.

That is a pretty poorly coded class. I suggest you keep looking at examples and learn the SOLID Principles.

You would do better to start here…

https://phpdelusions.net/pdo/pdo_wrapper

1 Like

Thanks for the direction, but in the early stage of learning, we often encounter such comments regarding the quality and authenticity of the shared code for some discussion(Sir, I am not cynical against your advice), but if possible please help me to understand where I am stuck in this part of the code or possible some suggested reading that can help me to understand that. This will help me to graduate further in some direction.

You would be wasting your time learning how to do it the “wrong” way. Check out the link I posted while you were responding.

As far as the posted code, the switch is just deciding what type of bind to use. If you use positional placeholders (?) then you done even need to mess with all that.

1 Like

Sure, Thanks. The link that you have shared says:

Although there are not too much reasons to create a wrapper (as PDO already being one), there are still some issues that might bug a programmer.

What does the wrapper mean?

Hi there, I am waiting for other experts’ opinions to tell if the original code posted by me(Not actually written by me) has a very poor class definition.

I am not doubting @benanamen but if possible other opinions will be very helpful.

A wrapper is just as the word means. A wrapper wraps around something, in this case, it wraps around the PDO Extension. PDO does not need a Wrapper as it is one.

1 Like

While you are waiting for “other opinions” it would be beneficial for you to know why this class is not good so you can learn to spot it in the future.

  1. First problem is the DB connection parameters are hard coded into the class. This means the class is “Tightly Coupled” meaning you can ONLY use that database/user/etc with this class. Because of that, the class is not re-usable without editing it which violates the Open/Closed Principle of the SOLID Principles. If you needed a different database at the same time you cant do it. These parameters should be passed to the Class using Dependency Injection.

  2. The options are hard coded. same kind of problems as the first.

  3. The class outputs internal system errors to the user. That info is only good to hackers and useless to the user.

  4. The bind method is just an unnecessary cluster muck. Using Positional placeholders would eliminate that whole block of code.

  5. The execute method is a useless wrapper.

  6. The result set method is hard coded to fetchAll and FETCH_ASSOC. There is no flexibility to do anything else.

All in all, the class is junk IMO.

Here is a tutorial on SOLID. I have not read it. but I have never seen a “wrong” tutorial on SOLID.

https://code.tutsplus.com/series/the-solid-principles–cms-634

2 Likes

Ok. The same Author has not hardcoded those parameters in his another video.
May be the other part of that class is still hard-coded as mentioned by you.

There is minor improvement in the video you linked to but then the author introduces a multitude of other bad practices throughout the code base.

I guess it can be a starting point but just keep in mind you would want to refactor the code to make it “right”.

1 Like

Make sense. Thanks.

What the switch is doing is testing the data type of the $value and switching the $data_type parameter of the bindValue method to match it.
This allows you to use the bind method, instead of PDO’s native bindValue method, and omit the $data_type parameter if you choose.
But given that the $data_type parameter is optional anyway, I’m not sure of the point of it, or any other methods in the class TBH, as they just seem to replicate what PDO already does, but in a more restrictive way. A wrapper class should enhance the functionality, not limit it.

3 Likes

9 posts were split to a new topic: Understanding PHP Router Scripts

Wrapper Class → When you say this you mean an extension class of the original PDO class defined in PHP?

You seem to be a Pro. I believe you must have written many MVC workflows so generally when as you stated a good class system for PDO is established/written can it be reused by in multiple projects.

If my assumption is true in that case it will be good to understand a well-written class and then re-use it multiple times or it lot depends upon the project type and such recurrence is not possible and is of no use of discussion.

I second the recommendation for phpdelusions.net. It helped me tremendously.

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.