Skip to main content
Code Review

Return to Question

Post Unlocked by Winston Ewert
Post Reopened by Winston Ewert
Post Closed as "off topic" by SylvainD, Winston Ewert
Post Locked by Community Bot
deleted 15 characters in body
Source Link
<?php
class Connection extends Mysqli{
 public function __construct($mysqli_host,$mysqli_user,$mysqli_pass, $mysqli_db) {
 parent::__construct($mysqli_host,$mysqli_user,$mysqli_pass,$mysqli_db);
 $this->throwConnectionExceptionOnConnectionError(); 
 }
 private function throwConnectionExceptionOnConnectionError(){
 if(!$this->connect_error){
 echo "Database connection established<br/>";
 }else{
 //$message = sprintf('(%s) %s', $this->connect_errno, $this->connect_error);
 echo "Error connecting to the database."; 
 throw new DatabaseException($message);
 }
 }
}
class DatabaseException extends Exception
{
}
class Page extends Mysqli{
 private $con; 
 public function __construct(Connection $con) {
 $this->con = $con;
 if(isset($_GET['id'])){
 $id = $_GET['id'];
 }else{ 
 $id = 1;
 } 
 $this->get_headers($id);
 $this->get_content($id);
 $this->get_footer($id);
 }
 private function get_headers($pageId){ 
 $retrieveHead = $this->con->prepare("SELECT headers FROM pages WHERE page_id=?");
 $retrieveHead->bind_param('i',$pageId);
 $retrieveHead->execute();
 $retrieveHead->bind_result($header);
 $retrieveHead->fetch();
 $retrieveHead->close();
 echo $header; 
 }
 private function get_footer($pageId){ 
 $retrieveFooter = $this->con->prepare("SELECT footer FROM pages WHERE page_id=?");
 $retrieveFooter->bind_param('i',$pageId);
 $retrieveFooter->execute();
 $retrieveFooter->bind_result($footer);
 $retrieveFooter->fetch();
 $retrieveFooter->close();
 echo $footer; 
 }
 private function get_content($pageId){
 $retreiveContent = $this->con->prepare("SELECT template_id, section_title, i1, i2 FROM content WHERE page_id=? ORDER BY sequence DESC");
 $retreiveContent->bind_param('i',$pageId);
 $retreiveContent->execute();
 $retreiveContent->bind_result($template_id, $section_title, $i1, $i2);
 $retreiveContent->store_result();
 while ($retreiveContent->fetch()) {
 //Variables will be populated for this row.
 //Update the tags in the template.
 $template = $this->get_template($template_id);
 $template = str_replace('[i1]',$i1,$template);
 $template = str_replace('[i2]',$i2,$template);
 //$theTemplate is populated with content. Probably want to echo here
 echo $template;
 }
 $retreiveContent->free_result(); 
 $retreiveContent->close();
 }
 private function get_template($template_id){
 $retreiveTemplate = $this->con->prepare("SELECT code FROM templates WHERE template_id=?");
 $retreiveTemplate->bind_param('i',$template_id);
 $retreiveTemplate->execute();
 $retreiveTemplate->bind_result($template);
 $retreiveTemplate->fetch();
 $retreiveTemplate->close();
 return $template;
 }
}
?>
<?php
class Connection extends Mysqli{
 public function __construct($mysqli_host,$mysqli_user,$mysqli_pass, $mysqli_db) {
 parent::__construct($mysqli_host,$mysqli_user,$mysqli_pass,$mysqli_db);
 $this->throwConnectionExceptionOnConnectionError(); 
 }
 private function throwConnectionExceptionOnConnectionError(){
 if(!$this->connect_error){
 echo "Database connection established<br/>";
 }else{
 //$message = sprintf('(%s) %s', $this->connect_errno, $this->connect_error);
 echo "Error connecting to the database."; 
 throw new DatabaseException($message);
 }
 }
}
class DatabaseException extends Exception
{
}
class Page extends Mysqli{
 private $con; 
 public function __construct(Connection $con) {
 $this->con = $con;
 if(isset($_GET['id'])){
 $id = $_GET['id'];
 }else{ 
 $id = 1;
 } 
 $this->get_headers($id);
 $this->get_content($id);
 $this->get_footer($id);
 }
 private function get_headers($pageId){ 
 $retrieveHead = $this->con->prepare("SELECT headers FROM pages WHERE page_id=?");
 $retrieveHead->bind_param('i',$pageId);
 $retrieveHead->execute();
 $retrieveHead->bind_result($header);
 $retrieveHead->fetch();
 $retrieveHead->close();
 echo $header; 
 }
 private function get_footer($pageId){ 
 $retrieveFooter = $this->con->prepare("SELECT footer FROM pages WHERE page_id=?");
 $retrieveFooter->bind_param('i',$pageId);
 $retrieveFooter->execute();
 $retrieveFooter->bind_result($footer);
 $retrieveFooter->fetch();
 $retrieveFooter->close();
 echo $footer; 
 }
 private function get_content($pageId){
 $retreiveContent = $this->con->prepare("SELECT template_id, section_title, i1, i2 FROM content WHERE page_id=? ORDER BY sequence DESC");
 $retreiveContent->bind_param('i',$pageId);
 $retreiveContent->execute();
 $retreiveContent->bind_result($template_id, $section_title, $i1, $i2);
 $retreiveContent->store_result();
 while ($retreiveContent->fetch()) {
 //Variables will be populated for this row.
 //Update the tags in the template.
 $template = $this->get_template($template_id);
 $template = str_replace('[i1]',$i1,$template);
 $template = str_replace('[i2]',$i2,$template);
 //$theTemplate is populated with content. Probably want to echo here
 echo $template;
 }
 $retreiveContent->free_result(); 
 $retreiveContent->close();
 }
 private function get_template($template_id){
 $retreiveTemplate = $this->con->prepare("SELECT code FROM templates WHERE template_id=?");
 $retreiveTemplate->bind_param('i',$template_id);
 $retreiveTemplate->execute();
 $retreiveTemplate->bind_result($template);
 $retreiveTemplate->fetch();
 $retreiveTemplate->close();
 return $template;
 }
}
?>
<?php
class Connection extends Mysqli{
 public function __construct($mysqli_host,$mysqli_user,$mysqli_pass, $mysqli_db) {
 parent::__construct($mysqli_host,$mysqli_user,$mysqli_pass,$mysqli_db);
 $this->throwConnectionExceptionOnConnectionError(); 
 }
 private function throwConnectionExceptionOnConnectionError(){
 if(!$this->connect_error){
 echo "Database connection established<br/>";
 }else{
 //$message = sprintf('(%s) %s', $this->connect_errno, $this->connect_error);
 echo "Error connecting to the database."; 
 throw new DatabaseException($message);
 }
 }
}
class DatabaseException extends Exception
{
}
class Page{
 private $con; 
 public function __construct(Connection $con) {
 $this->con = $con;
 if(isset($_GET['id'])){
 $id = $_GET['id'];
 }else{ 
 $id = 1;
 } 
 $this->get_headers($id);
 $this->get_content($id);
 $this->get_footer($id);
 }
 private function get_headers($pageId){ 
 $retrieveHead = $this->con->prepare("SELECT headers FROM pages WHERE page_id=?");
 $retrieveHead->bind_param('i',$pageId);
 $retrieveHead->execute();
 $retrieveHead->bind_result($header);
 $retrieveHead->fetch();
 $retrieveHead->close();
 echo $header; 
 }
 private function get_footer($pageId){ 
 $retrieveFooter = $this->con->prepare("SELECT footer FROM pages WHERE page_id=?");
 $retrieveFooter->bind_param('i',$pageId);
 $retrieveFooter->execute();
 $retrieveFooter->bind_result($footer);
 $retrieveFooter->fetch();
 $retrieveFooter->close();
 echo $footer; 
 }
 private function get_content($pageId){
 $retreiveContent = $this->con->prepare("SELECT template_id, section_title, i1, i2 FROM content WHERE page_id=? ORDER BY sequence DESC");
 $retreiveContent->bind_param('i',$pageId);
 $retreiveContent->execute();
 $retreiveContent->bind_result($template_id, $section_title, $i1, $i2);
 $retreiveContent->store_result();
 while ($retreiveContent->fetch()) {
 //Variables will be populated for this row.
 //Update the tags in the template.
 $template = $this->get_template($template_id);
 $template = str_replace('[i1]',$i1,$template);
 $template = str_replace('[i2]',$i2,$template);
 //$theTemplate is populated with content. Probably want to echo here
 echo $template;
 }
 $retreiveContent->free_result(); 
 $retreiveContent->close();
 }
 private function get_template($template_id){
 $retreiveTemplate = $this->con->prepare("SELECT code FROM templates WHERE template_id=?");
 $retreiveTemplate->bind_param('i',$template_id);
 $retreiveTemplate->execute();
 $retreiveTemplate->bind_result($template);
 $retreiveTemplate->fetch();
 $retreiveTemplate->close();
 return $template;
 }
}
?>
Changed title to better reflect the question
Link

Object Orientated PHP, what's wrong with this setupimplementation of OOPHP and how might it be improved?

edited tags
Link
Post Migrated Here from programmers.stackexchange.com (revisions)
Source Link
Loading
lang-php

AltStyle によって変換されたページ (->オリジナル) /