Skip to main content
Code Review

Return to Question

replaced http://codereview.stackexchange.com/ with https://codereview.stackexchange.com/
Source Link

I'm an absolute beginner in PHP OOP in search of the "Holy Grail" of connecting to MySQL database once and reusing this connection for the whole site.

classes/db.php

<?php
define('SERVER', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', 'password');
define('DATABASE', 'cms');
class DB {
 function __construct(){
 $connection = @mysql_connect(SERVER, USERNAME, PASSWORD) or die('Connection error -> ' . mysql_error());
 mysql_select_db(DATABASE, $connection) or die('Database error -> ' . mysql_error());
 }
}
?>

classes/users.php

<?php
class Users {
 function __construct(){
 $db = new DB();
 }
 
 public function read(){
 $query = mysql_query("select use_id, use_name, use_email FROM users");
 while ($row = mysql_fetch_array($query)){
 $data[] = $row;
 }
 return $data;
 }
}
?>

users.php

<?php require_once('classes/db.php'); ?>
<?php require_once('classes/users.php'); ?>
<?php
$Users = new Users();
$users = $Users->read();
?>
<?php
if ($users) {
 echo '<ul>';
 foreach($users as $user){
 echo '<li><a href="mailto:' . $user['use_email'] . '">' . $user['use_name'] . '</a></li>';
 }
 echo '</ul>'; 
}
?>

My doubts are mostly on the Users class part:

function __construct(){
 $db = new DB();
}

It seems to be an easy way to have the connection available, but I read somewhere that instantiate the db connection in the constructor is a bad idea. Can you explain why, and if there's a better way to have the db connection easily available in every class that needs it?

I read a similar question here here, but I can't understand the abstraction/holding reference/singleton suggestion from the accepted answer, and the lack of a full practical example doesn't help me.

I'm an absolute beginner in PHP OOP in search of the "Holy Grail" of connecting to MySQL database once and reusing this connection for the whole site.

classes/db.php

<?php
define('SERVER', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', 'password');
define('DATABASE', 'cms');
class DB {
 function __construct(){
 $connection = @mysql_connect(SERVER, USERNAME, PASSWORD) or die('Connection error -> ' . mysql_error());
 mysql_select_db(DATABASE, $connection) or die('Database error -> ' . mysql_error());
 }
}
?>

classes/users.php

<?php
class Users {
 function __construct(){
 $db = new DB();
 }
 
 public function read(){
 $query = mysql_query("select use_id, use_name, use_email FROM users");
 while ($row = mysql_fetch_array($query)){
 $data[] = $row;
 }
 return $data;
 }
}
?>

users.php

<?php require_once('classes/db.php'); ?>
<?php require_once('classes/users.php'); ?>
<?php
$Users = new Users();
$users = $Users->read();
?>
<?php
if ($users) {
 echo '<ul>';
 foreach($users as $user){
 echo '<li><a href="mailto:' . $user['use_email'] . '">' . $user['use_name'] . '</a></li>';
 }
 echo '</ul>'; 
}
?>

My doubts are mostly on the Users class part:

function __construct(){
 $db = new DB();
}

It seems to be an easy way to have the connection available, but I read somewhere that instantiate the db connection in the constructor is a bad idea. Can you explain why, and if there's a better way to have the db connection easily available in every class that needs it?

I read a similar question here, but I can't understand the abstraction/holding reference/singleton suggestion from the accepted answer, and the lack of a full practical example doesn't help me.

I'm an absolute beginner in PHP OOP in search of the "Holy Grail" of connecting to MySQL database once and reusing this connection for the whole site.

classes/db.php

<?php
define('SERVER', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', 'password');
define('DATABASE', 'cms');
class DB {
 function __construct(){
 $connection = @mysql_connect(SERVER, USERNAME, PASSWORD) or die('Connection error -> ' . mysql_error());
 mysql_select_db(DATABASE, $connection) or die('Database error -> ' . mysql_error());
 }
}
?>

classes/users.php

<?php
class Users {
 function __construct(){
 $db = new DB();
 }
 
 public function read(){
 $query = mysql_query("select use_id, use_name, use_email FROM users");
 while ($row = mysql_fetch_array($query)){
 $data[] = $row;
 }
 return $data;
 }
}
?>

users.php

<?php require_once('classes/db.php'); ?>
<?php require_once('classes/users.php'); ?>
<?php
$Users = new Users();
$users = $Users->read();
?>
<?php
if ($users) {
 echo '<ul>';
 foreach($users as $user){
 echo '<li><a href="mailto:' . $user['use_email'] . '">' . $user['use_name'] . '</a></li>';
 }
 echo '</ul>'; 
}
?>

My doubts are mostly on the Users class part:

function __construct(){
 $db = new DB();
}

It seems to be an easy way to have the connection available, but I read somewhere that instantiate the db connection in the constructor is a bad idea. Can you explain why, and if there's a better way to have the db connection easily available in every class that needs it?

I read a similar question here, but I can't understand the abstraction/holding reference/singleton suggestion from the accepted answer, and the lack of a full practical example doesn't help me.

deleted 58 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

I'm an absolute beginner onin PHP OOP in search of the "Holy Graal"Grail" of connecting to MySQL database once and reusereusing this connection for the whole site So far, I've created these files:.

classes/db.phpclasses/db.php

<?php
define('SERVER', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', 'password');
define('DATABASE', 'cms');
class DB {
 function __construct(){
 $connection = @mysql_connect(SERVER, USERNAME, PASSWORD) or die('Connection error -> ' . mysql_error());
 mysql_select_db(DATABASE, $connection) or die('Database error -> ' . mysql_error());
 }
}
?>

classes/users.phpclasses/users.php

<?php
class Users {
 function __construct(){
 $db = new DB();
 }
 
 public function read(){
 $query = mysql_query("select use_id, use_name, use_email FROM users");
 while ($row = mysql_fetch_array($query)){
 $data[] = $row;
 }
 return $data;
 }
}
?>

users.phpusers.php

<?php require_once('classes/db.php'); ?>
<?php require_once('classes/users.php'); ?>
<?php
$Users = new Users();
$users = $Users->read();
?>
<?php
if ($users) {
 echo '<ul>';
 foreach($users as $user){
 echo '<li><a href="mailto:' . $user['use_email'] . '">' . $user['use_name'] . '</a></li>';
 }
 echo '</ul>'; 
}
?>

Well, myMy doubts are mostly on the UsersUsers class part:

function __construct(){
 $db = new DB();
}

It seems to mebe an easy way to have the connection available, but I read somewhere that instantiate the db connection in the constructor is a bad idea: please, can. Can you explain why, and if there's a better way to have the db connection easily available in every class that needs it?

I read a similar question here, but I can't understand the abstraction/holding reference/singleton suggestion from the accepted answer, and the lack of a full practical example doesn't help me...

Thanks a lot in advance

I'm an absolute beginner on PHP OOP in search of the "Holy Graal" of connecting to MySQL database once and reuse this connection for the whole site So far, I've created these files:

classes/db.php

<?php
define('SERVER', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', 'password');
define('DATABASE', 'cms');
class DB {
 function __construct(){
 $connection = @mysql_connect(SERVER, USERNAME, PASSWORD) or die('Connection error -> ' . mysql_error());
 mysql_select_db(DATABASE, $connection) or die('Database error -> ' . mysql_error());
 }
}
?>

classes/users.php

<?php
class Users {
 function __construct(){
 $db = new DB();
 }
 
 public function read(){
 $query = mysql_query("select use_id, use_name, use_email FROM users");
 while ($row = mysql_fetch_array($query)){
 $data[] = $row;
 }
 return $data;
 }
}
?>

users.php

<?php require_once('classes/db.php'); ?>
<?php require_once('classes/users.php'); ?>
<?php
$Users = new Users();
$users = $Users->read();
?>
<?php
if ($users) {
 echo '<ul>';
 foreach($users as $user){
 echo '<li><a href="mailto:' . $user['use_email'] . '">' . $user['use_name'] . '</a></li>';
 }
 echo '</ul>'; 
}
?>

Well, my doubts are mostly on the Users class part:

function __construct(){
 $db = new DB();
}

It seems to me an easy way to have the connection available, but I read somewhere that instantiate the db connection in the constructor is a bad idea: please, can you explain why, and if there's a better way to have the db connection easily available in every class that needs it?

I read a similar question here, but I can't understand the abstraction/holding reference/singleton suggestion from the accepted answer, and the lack of a full practical example doesn't help me...

Thanks a lot in advance

I'm an absolute beginner in PHP OOP in search of the "Holy Grail" of connecting to MySQL database once and reusing this connection for the whole site.

classes/db.php

<?php
define('SERVER', 'localhost');
define('USERNAME', 'root');
define('PASSWORD', 'password');
define('DATABASE', 'cms');
class DB {
 function __construct(){
 $connection = @mysql_connect(SERVER, USERNAME, PASSWORD) or die('Connection error -> ' . mysql_error());
 mysql_select_db(DATABASE, $connection) or die('Database error -> ' . mysql_error());
 }
}
?>

classes/users.php

<?php
class Users {
 function __construct(){
 $db = new DB();
 }
 
 public function read(){
 $query = mysql_query("select use_id, use_name, use_email FROM users");
 while ($row = mysql_fetch_array($query)){
 $data[] = $row;
 }
 return $data;
 }
}
?>

users.php

<?php require_once('classes/db.php'); ?>
<?php require_once('classes/users.php'); ?>
<?php
$Users = new Users();
$users = $Users->read();
?>
<?php
if ($users) {
 echo '<ul>';
 foreach($users as $user){
 echo '<li><a href="mailto:' . $user['use_email'] . '">' . $user['use_name'] . '</a></li>';
 }
 echo '</ul>'; 
}
?>

My doubts are mostly on the Users class part:

function __construct(){
 $db = new DB();
}

It seems to be an easy way to have the connection available, but I read somewhere that instantiate the db connection in the constructor is a bad idea. Can you explain why, and if there's a better way to have the db connection easily available in every class that needs it?

I read a similar question here, but I can't understand the abstraction/holding reference/singleton suggestion from the accepted answer, and the lack of a full practical example doesn't help me.

Tweeted twitter.com/#!/StackCodeReview/status/131420344660656129
edited tags
Link
palacsint
  • 30.3k
  • 9
  • 82
  • 157
Source Link
Ivan
  • 185
  • 1
  • 2
  • 6
Loading
default

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