Hi,

As i am new in OOP, i just want to ask those who are experts in OOP that can this my example be considered as OOP example?

Thanks

dbprocess.class.php

<?php
class DBprocess {
	private $dbconn;
	private $db;
	
	public function __construct($host, $user, $username, $password, $database) {
		$this->dbconn=mysql_connect($host, $user, $username, $password);
		$this->db=mysql_select_db($database);
	}
	
	private function executeQuery($sql, $process) {
		$runSql=mysql_query($sql, $this->dbconn) or die ("ERROR: Database connection error");
		
		if ($process == "find" || $process == "list") {
			if(@mysql_num_rows($runSql)>0) {
				while($newArray=mysql_fetch_assoc($runSql)) {
					$data[]=$newArray;
				}
				return $data;
			} else {
				return "No Record";
			}
		} else if ($process == "insert" || $process == "delete" || $process == "update") {
			if (mysql_affected_rows()>0) {
				return "Process is done";
			} else {
				return "Try again later";
			}
		}
	}
		
	public function findRec($id) {
		$sql="SELECT * FROM person WHERE id='$id'";
		return $this->executeQuery($sql, "find");
	}
	
	public function listRec() {
		$sql="SELECT * FROM person";
		return $this->executeQuery($sql, "list");
	}

	public function insertRec($name, $surname) {
		$sql="INSERT INTO person (name, surname) VALUES ('$name', '$surname')";
		return $this->executeQuery($sql, "insert");
	}

	public function deleteRec($id) {
		$sql="DELETE FROM person WHERE id='$id'";
		return $this->executeQuery($sql, "delete");
	}

	public function updateRec($id, $name, $surname) {
		$sql="UPDATE person SET name='$name', surname='$surname' WHERE id='$id'";
		return $this->executeQuery($sql, "update");
	}
}
?>
index.php

<?php
require_once("dbprocess.class.php");

$result=new DBprocess("localhost", "root", null, null, "classTest");

/* FIND
$data=$result->findRec(4);
echo "<pre>";
print_r($data);
echo "</pre>";
*/

/* LIST
$data=$result->listRec();
echo "<pre>";
print_r($data);
echo "</pre>";
*/

/* INSERT
$data=$result->insertRec("Robert", "De Niro");
print_r($data);
*/

/* DELETE
$data=$result->deleteRec(1);
print_r($data);
*/

/* UPDATE
$data=$result->deleteRec(1, "Ali", "Veli");
print_r($data);
*/

?>

Recommended Answers

All 3 Replies

Yes, that is oop.

Yes I agree.
How to confirm, the use of the -> symbol combination and also with the use of the terms public function and private function

Why do you keep adding extra parameters to mysql_connect? it only takes hostname, username, password. You're adding two usernames for no reason.

You've also broken encapsulation by making a base class (DBprocess) too specific by entering into one table. There should be child classes which extend the base class that specialize. The base class should be just that: generic.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.