php 创建class的疑惑

来源:百度知道 编辑:UC知道 时间:2024/07/01 05:36:53
创建一个class中必须要有function吗?
譬如
class my_db
{
public $server_host='localhost';
public $user_name='root';
public $password='';
public $conn=mysql_connect($this->server_host,$this->user_name,$this->password);
if(!$conn)
{
die("Could not connect".mysql_error());
}
}
这样就报错:
Parse error: parse error, expecting `','' or `';'' in D:\Web\wwwroot\index.php on line 8
就是这行错误:
public $conn=mysql_connect($this->server_host,$this->user_name,$this->password);
为什么呢?
弄明白了,因为我在这里面加上了方法。class只能在function中加上方法的。

ma di, 我昨天晚上也碰到了这个情况;

mysql_connect这个本来就是个系统函数,不要function啊。

其他的自定义函数要加上function。

加上
function _GET($varname)
{
return $this->$varname;
}

function _SET($varname,$value)
{
$this->$varname=$value;
}

试试看,昨天晚上学的。

要不你就把mysql_connect 这里的$this->去掉。