怎么给php函数添加class样式

2025-03-26 21:49:58
推荐回答(3个)
回答1:

……函数没有样式,样式是指html标签中的css样式。

而php中的class 是指类,把一些常用的功能封装到一个类中,方便调用。声明一个类的方法例如:(这里我们封装一个数据库连接类)

class mysql{ //声明数据连接类,取名加mysql
    var $localhost; //声明成员属性
    var $name;
    var $pwd;
    var $db;
    var $conn;
    public function mysql($localhost,$name,$pwd,$db){//声明构造方法
        $this->localhost=$localhost;//赋值
        $this->name=$name;
        $this->pwd=$pwd;
        $this->db=$db;
        $this->connect();//访问该类中的connect方法
    }
    public function connect(){//定义connect方法
        $this->conn=$conn;
        $this->conn=mysql_connect($this->localhost,$this->name,$this->pwd) or
                    die("连接错误!");
        mysql_select_db($this->db,$this->conn) or die("选择数据库错误!");
        mysql_query("SET NAME UTF-8");
    }
}
//实例化 mysql类,这里就调用了这个数据库连接类,完成了连接数据库和选择数据库的工作
$msl=new mysql("127.0.0.1","root","123456","test_db");
?>

当然实际编写中不用这么麻烦,我们可以是用现成的PHP框架,如:thinkphp等。这些类框架已经帮我们封装好了,直接调用即可。

回答2:

只能给html加样式。PHP可以输出html字符。所以你看看你的php程序输出了什么东西,在跟据输出的html结构写样式

回答3:

class txt{
public function aaa(){
echo 111;

}

}

$model = new txt;
$model->aaa();