a question about php...
时间:2010-08-18 来源:tyjhField
I learn about php for several days, today I find a question, which I think is very interesting.
Use php for web development
Project's name: firstwebsite
Architecture of project :
1.entities(folder):all the entity classes are put in it,such as Method,which is abstract of experiment method.As follows,"Method.class.php":
<?php
//方法的实体对象
class Method{
//编号
private $method_id;
//名称
private $method_name;
public function __get($name){
return $this->$name;
}
public function __set($name,$value){
$this->$name = $value;
}
}
?>
2.dao(folder):all the database access objects directorly are in it.
such as "DBConnection.class.php" , just for DB connection:
<?php
//数据库连接类
class DBConnection{
//主机名,服务器名称
private static $domain = "localhost";
//数据库用户名
private static $username = "haha";
//数据库密码
private static $passwd = "haha";
//数据库名称
private $database = "bioinformation";
public static function get_connection(){
return new mysqli("localhost","haha","haha","information");
}
}
?>
as for entities, "Method_Dao.class.php", do the basic interactiones with DB:
<?php
require("DBConnection.class.php");
//Method实体对象的数据库层操作类
class Method_Dao{
private $mysqli;//数据库连接对象(资源)
private $method;//方法实体对象
//构造函数
function __construct(){
$this->mysqli = DBConnection::get_connection();//得到数据连接资源(对象)
}
function __set($name,$value){
$this->$name = $value;
}
//增加一条记录,增加成功返回true,否则返回false
function add_one_method(){
try{
$insert = "insert into method(METHOD_NAME) VALUES (?)";
$stmt = $this->mysqli->prepare($insert);
/*这里有个重要的问题,在参数绑定的时候,参数是不能有效地传入数据的,必须要再赋值
**$stmt->bind_param('s',$method->method_name);
**$stmt->execute();
**数据将无法插入,而采用下面的写法方可
**但是若采用
**$stmt->bind_param('s',$this->method->method_name);
**$stmt->execute();
**这样数据还是可以插入的,但是页面会提示
**Notice: Indirect modification of overloaded property Method::$method_name has no effect in **********
**Said as tyjh.csdn
*/
$stmt->bind_param('s',$method_name);
$method_name = $this->method->method_name;
$stmt->execute();
$stmt->close();
return true;
}
catch(Exception $e){
return false;
}
}
//删除一条记录,成功返回true,否则返回false
function delete_method_by_ID(){
try{
$delete = "delete from method where METHOD_ID=".$this->method->method_id;
$this->mysqli->query($delete);
return true;
}
catch(Exception $e){
return false;
}
}
function __destructor(){
}
}
?>
3.pages(folder):web pages.
Just for test the Method_Dao.class:
<html>
<meta http-equiv="content-type" content="text/html;charset=UTF-8">
<?php
require("./dao/Method_Dao.class.php");
require("./entities/Method.class.php");
$methodDao= new Method_Dao();
$method = new Method();
$method->method_id = 1;
$method->method_name="method2";
$methodDao->method=$method;
if($methodDao->add_one_method()) echo "insert one record KO!";
else echo "insert failuer";
?>
</html>
From those, I may recognise that class Method_Dao's functions can not access Method_Dao's properties:
/*这里有个重要的问题,在参数绑定的时候,参数是不能有效地传入数据的,必须要再赋值
**$stmt->bind_param('s',$method->method_name);
**$stmt->execute();
**数据将无法插入,
but we can use "$this->method" to access:
**但是若采用
**$stmt->bind_param('s',$this->method->method_name);
**$stmt->execute();
**这样数据还是可以插入的
How can we explain this phenomena? Let tall about it.
Thanks for your attention.