php with two class
my sample is good or 开发者_如何学JAVAnot? I have a good connection to the database, or too should be in the class?
Thanks
<?php
mysql_connect('localhost','root','admin');
mysql_select_db('test');
class UserDisplay
{
function getDisplayName()
{
$sql = 'select first_name, last_name, display_name from users where user_id = "3"';
$results = mysql_query($sql);
$row = mysql_fetch_array($results);
$this->user_id = $user_id;
return $this->user_id;
}
}
class UserInsert
function InsertName($name)
{
mysql_query("INSERT INTO Persons (first_name)VALUES ('".$name."')");
}
}
$userD = new UserDisplay();
echo "User known as: " . $userD->getDisplayName() . "\n";
$userI = new UserInsert();
$userI->InsertName("Peter");
?>
You should merge those classes into a single User class, then select the user array from the database in the constructor and store it in a class variable.
You would also ideally pass in the mysql connection to that class so that it doesn't always just use the default connection. The mysql functions all have a "link identifier" parameter, and it's considered good practice to use it if you aren't using an OO interface (like mysqli's OO class).
Probably be better off just making them functions, instead of wrapping them up in classes. Extract all of it, except the last into it's own php file. You forgot to extract display name from $user
// myFunctions.php
<?php
function connectDb() {
mysql_connect('localhost', 'root', 'admin');
mysql_select_db('test');
}
function closeDb() {
mysql_close();
}
function displayName() {
$sql = 'select first_name, last_name, display_name from users where user_id = "3"';
$results = mysql_query($sql);
$user = mysql_fetch_array($results);
return $user['display_name'];
}
function insertFirstName($name) {
mysql_query("INSERT INTO Persons (first_name)VALUES ('" . $name . "')");
}
?>
// index.php
<?php
require_once 'myFunctions.php';
connectDb();
echo "User known as: " . displayName() . "\n";
insertFirstName("Peter");
closeDb();
?>
But if you insist on using classes:
// User.php
<?php
class User {
private function connectDb() {
mysql_connect('localhost', 'root', 'admin');
mysql_select_db('test');
}
private function closeDb() {
mysql_close();
}
function displayName() {
$this->connectDB();
$sql = 'select first_name, last_name, display_name from users where user_id = "3"';
$results = mysql_query($sql);
$user = mysql_fetch_array($results);
$this->closeDB();
return $user['display_name'];
}
function insertFirstName($name) {
$this->connectDB();
mysql_query("INSERT INTO Persons (first_name)VALUES ('" . $name . "')");
$this->closeDB();
}
}
?>
// index.php
<?php
require_once 'User.php';
$user = new User;
echo "User known as: " . $user->displayName() . "\n";
$user->insertFirstName("Peter");
?>
Actually not bad. abstracted out the db connection and close.
Your sample is not really ideal. Classes should not depend on or modify any external resources, except whatever has been passed to them through arguments / properties / or method calls.
精彩评论