本文目录一览:
1、php 类怎么写 2、使用PHP写一个类和一个控制器 3、php 如何写类 4、php类怎么写? 5、php 类的写法 6、如何在PHP中定义一个类
php 类怎么写
class save_reg {
private $usernc;
private $userpwd;
private $phone;
private $email;
public function __construct($username, $userpwd, $phone, $email) {
$this->usernc = $username;
$this->userpwd = $userpwd;
$this->phone = $phone;
$this->email = $email;
}
public function savereg() {
$conn = mysql_connect("localhost", "root", "123456");
$sql = mysql_selectdb("select", $conn);
mysql_query("set names gb2312");
if ($_POST['button'] == TRUE) {
if ($conn && $sql) {
if (!($_POST['usernc'] && $_POST['pwd'])) {
echo "用户名或密码不能为空。单击 <a href='javascript:onclick=history.go(-1)'>这里</a> 返回<br>";
} else {
echo "数据库连接成功!<br>";
echo "数据库选择成功!<br>";
$relest = mysql_query("select username from student where username='" . $this->usernc . "'", $conn);
if (mysql_fetch_array($relest)) {
echo "<script>alert('该用户名已被占用!');history.back();</script>";
exit;
} else {
if (mysql_query("insert into student (username,userpwd,phone,email ) values ('" . $this->usernc . "','" . $this->userpwd . "','" . $this->phone . "','" . $this->email . "')", $conn)) {
mysql_free_result($relest);
mysql_close($conn);
echo "<script>alert('添加成功!');history.back();</script>";
}
}
}
}
}
}
}
使用PHP写一个类和一个控制器
<?php
/**
* 以下代码用于数据库操作类的封装
*
* @author rexrex.sp.li@aliyun.com
* @version 1.0
* @since 2015
*/
class Mysql {
// 数据库连接返回值
private $conn;
/**
* [构造函数,返回值给$conn]
* @param [string] $hostname [主机名]
* @param [string] $username [用户名]
* @param [string] $password [密码]
* @param [string] $dbname [数据库名]
* @param [string] $charset [字符集]
* @return [null]
*/
function __construct($hostname, $username, $password, $dbname, $charset = 'utf8') {
$conn = @mysql_connect($hostname, $username, $password);
if (!$conn) {
echo '连接失败,请联系管理员';
exit;
}
$this->conn = $conn;
$res = mysql_select_db($dbname);
if (!$res) {
echo '连接失败,请联系管理员';
exit;
}
mysql_set_charset($charset);
}
function __destruct() {
mysql_close();
}
/**
* [getAll 获取所有信息]
* @param [string] $sql [sql语句]
* @return [array] [返回二维数组]
*/
function getAll($sql) {
$result = mysql_query($sql, $this->conn);
$data = array();
if ($result && mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$data[] = $row;
}
}
return $data;
}
/**
* [getOne 获取单条数据]
* @param [string] $sql [sql语句]
* @return [array] [返回一维数组]
*/
function getOne($sql) {
$result = mysql_query($sql, $this->conn);
$data = array();
if ($result && mysql_num_rows($result) > 0) {
$data = mysql_fetch_assoc($result);
}
return $data;
}
/**
* [getOne 获取单条数据]
* @param [string] $table [表名]
* @param [string] $data [由字段名当键,属性当键值的一维数组]
* @return [type] [返回false或者插入数据的id]
*/
function insert($table, $data) {
$str = '';
$str .= "INSERT INTO `$table` ";
$str .= "(`" . implode("`,`", array_keys($data)) . "`) ";
$str .= " VALUES ";
$str .= "('".implode("','", $data)."')";
$res = mysql_query($str, $this->conn);
if ($res && mysql_affected_rows() > 0) {
return mysql_insert_id();
} else {
return false;
}
}
/**
* [update 更新数据库]
* @param [string] $table [表名]
* @param [array] $data [更新的数据,由字段名当键,属性当键值的一维数组]
* @param [string] $where [条件,‘字段名’=‘字段属性’]
* @return [type] [更新成功返回影响的行数,更新失败返回false]
*/
function update($table, $data, $where) {
$sql = 'UPDATE ' . $table . ' SET ';
foreach ($data as $key => $value) {
$sql .= "`{$key}`='{$value}',";
}
$sql = rtrim($sql, ',');
$sql .= " WHERE $where";
$res = mysql_query($sql, $this->conn);
if ($res && mysql_affected_rows()) {
return mysql_affected_rows();
} else {
return false;
}
}
/**
* [delete 删除数据]
* @param [string] $table [表名]
* @param [string] $where [条件,‘字段名’=‘字段属性’]
* @return [type] [成功返回影响的行数,失败返回false]
*/
function del($table, $where) {
$sql = "DELETE FROM `{$table}` WHERE {$where}";
$res = mysql_query($sql, $this->conn);
if ($res && mysql_affected_rows()) {
return mysql_affected_rows();
} else {
return false;
}
}
}
实例化类:
<?php
// 包含数据库操作类文件
include 'mysql.class.php';
// 设置传入参数
$hostname = 'localhost';
$username = 'root';
$password = '123456';
$dbname = 'aisi';
$charset = 'utf8';
// 实例化对象
$db = new Mysql($hostname, $username, $password, $dbname);
// 获取一条数据
$sql = "SELECT count(as_article_id) as count FROM as_article where as_article_type_id=1";
$count = $db->getOne($sql);
// 获取多条数据
$sql = "SELECT * FROM as_article where as_article_type_id=1 order by as_article_addtime desc limit $start,$limit";
$service = $db->getAll($sql);
// 插入数据
$arr = array(
'as_article_title' => '数据库操作类',
'as_article_author' => 'rex',
);
$res = $db->insert('as_article', $arr);
// 更新数据
$arr = array(
'as_article_title' => '实例化对象',
'as_article_author' => 'Lee',
);
$where = "as_article_id=1";
$res = $db->update('as_article', $arr, $where);
// 删除数据
$where = "as_article_id=1";
$res = $db->del('as_article', $where);
?>
getOne
方法传入 $sql
的 SQL 语句用于查询单条数据并返回一维数组;getAll
方法同样传入 SQL 语句,用于查询多条数据,并返回二维数组;insert
方法传入表名和关联数组,返回布尔型或者插入数据对应索引;update
方法传入表名、关联数组和条件,返回布尔型或者影响的行数;del
方法传入表名和条件,返回布尔型。
php 如何写类
面向对象编程
class a {
// 类成员
var $aaa;
// 类方法
function b($abc) {
$this->aaa = $abc;
}
}
$c = new a(); // 调用类
echo $c->b("方法");
php类怎么写?
<?php
class ClassName {
public $name = 'ClassName';
protected $_version = '1.0';
private $_author = 'incNick';
public function a() {}
protected function _b() {}
private function _c() {}
}
class Children extends ClassName {}
var
是 PHP4 中的用法,相当于 public
,PHP4 中不支持 protected
等。
public
关键字:公共访问的属性、方法protected
关键字:类及子类内公共访问的属性、方法private
关键字:当前类内私有的属性、方法final
关键字:最终的,子类中不允许覆盖。如果是final class
,该类不可被继承。static
关键字:静态属性、方法,如public static $abc
const
关键字:常量属性,如const ABC = 'test'
更多解释请参考手册。
php 类的写法
部分同意楼上的,不知道 2300380 用的是什么语言 -_-!!!
PHP 字符串链接用的是 .
。
PHP 可以用 var
声明变量(仅在类中,相当于 public
,高版本的 PHP 中,老版的只有用 var
声明)。
代码过程基本就是一楼写的,改进一下:
class daxiang {
var $name = "";
function zou($name) {
$this->name = $name;
echo ("大象" . $this->name . "在走路");
}
}
"this"
是 PHP 中特有的一个特殊对象,你可以在 PHP 的类的程序中大量的看到,它指“自已”这个对象。
如何在PHP中定义一个类
当你声明一个类,你需要列出对象应有的所有变量和所有函数(被称为属性和方法)。图1中显示了一个类的构成。注意在大括号 ({}
) 内你只能声明变量或者函数。图2中显示了如何在一个类中定义三个属性和两个方法。
以下为引用的内容:
class Name extends AnotherClass {
Access Variable Declaration
Access Function Declaration
}
class User {
public $name;
public $password;
public $lastLogin;
public $accesses = 0;
function __construct($name, $password) {
$this->name = $name;
$this->password = $password;
$this->lastLogin = time();
$this->accesses++;
}
// 获取最后访问的时间
function getLastLogin() {
return(date("M d Y", $this->lastLogin));
}
}
// 创建一个对象的实例
$user = new User("Leon", "sdf123");
// 获取最后访问的时间
print($user->getLastLogin() . "\n");
// 打印用户名
print("$user->name\n");
当你声明属性时,你不需要指明数据类型。变量可能是整型、字符串或者是另一个对象,这取决于实际情况。在声明属性时增加注释是一个好主意,标记上属性的含义和数据类型。
当你声明一个方法时,你所做的和在类外部定义一个函数是一样的。方法和属性都有各自的命名空间。这意味着你可以安全地建立一个与类外部函数同名的方法,两者不会冲突。例如,一个类中可以定义一个名为 date()
的方法。但是你不能将一个方法命名为 PHP 的关键字,如 for
或者 while
。
类方法可能包含 PHP 中所谓的 type hint。Type hint 是另一个传递参数给方法的类的名字。如果你的脚本调用方法并传递一个不是类的实例的变量,PHP 将产生一个“致命 (fatal) 错误”。你可能没有给其它类型给出 type hint,就像整型、字符串、或者布尔值。在书写的时候,type hint 是否应当包含数组类型仍存在争议。
Type hint 是测试函数参数或者运算符的实例的数据类型的捷径。你可能总是返回这个方法。确认你强制让一个参数必须是哪种数据类型,如整型。图3 确保编译类只产生 Widget 的实例。
以下为引用的内容:
<?php
// 组件
class Widget {
public $name = 'none';
public $created = FALSE;
}
// 装配器
class Assembler {
public function make(Widget $w) {
print("Making $w-name\n");
$w->created = TRUE;
}
}
// 建立一个组件对象
$thing = new Widget;
$thing->name = 'Gadget';
// 装配组件
Assembler::make($thing);
?>
除了传递参数的变量外,方法含有一个特殊的变量 $this
。它代表类的个别实例。你应当用这个来指向对象的属性和其它方法。一些面向对象的语言假设一个不合格的变量提交给本地属性,但在 PHP 中方法的任何变量只是在方法的一定范围内。注意在 User 类的构造函数中这个变量的使用图2。