文秘频道 | 图片频道 | 教程频道 | 教案频道 | 加入收藏 | 会员投稿
当前位置:教程荟萃首页 >> 编程开发 >> PHP >> php七天入门教程第七天
php七天入门教程第七天
2008-03-25 13:30:12  作者:  来源:互联网  浏览次数:1  文字大小:【】【】【

面向对象的php 字串7
/*
字串5
//////////////////////////////////////
@ author:yangfan
@ date :2008/03/13
@ title:面向对象的php
*/ 字串7
//面向对象
字串5
class classname2
{
 var $a;
 function op($p){
  $this->a = $p;
  echo $this->a;
 }
}
$dd  = new classname2();
$dd->op('good'); 字串1
//实战 php面向对象! 字串4
class Student{
 //类的对象
 var $name;
 var $sex;
 var $age;
 //构造方法
 function __construct($name,$sex,$age){
  $this->name = $name;
  $this->sex = $sex;
  $this->age = $age;
 }
 //定义一个没参数的函数
 function talk(){
  echo "
名字:".$this->name."年龄:".$this->age."性别:".$this->sex."
";
 }
 //销毁
 
}
//定义学生
$a = new Student("yangfan","boy", 19);
$b = new Student("xinxing","girl",20);
$c = new Student("luofei","girl",19);
$a->talk();
$b->talk();
$c->talk(); 字串9
class Person
{
//下面是人的成员属性
var $name;   //人的名子
var $sex;   //人的性别
var $age;   //人的年龄
//定义一个构造方法参数为姓名$name、性别$sex和年龄$age
function __construct ($name, $age, $sex){
  $this->name = $name;
  $this->age = $age;
  $this->sex = $sex;
}
//这个人的说话方法
function say()
{
echo "我的名子叫:".$this->name." 性别:".$this->sex." 我的年龄是:".$this->age."
";
}
}
//通过构造方法创建3个对象$p1、p2、$p3,分别传入三个不同的实参为姓名、性别和年龄
$p1=new Person("张三","男", 20);
$p2=new Person("李四","女", 30);
$p3=new Person("王五","男", 40); 字串2
echo $p1->name."
";
echo $p1->sex."
";
echo $p1->age."
"; 字串9
  字串5
echo "
-----------------------------------------------------
"; 字串4
class stu {
 private $name;
 private $age;
 private $sex;
 
 //!!!!!!!!!!!!!!!!!!!!特别注意:construct前面有2个'_'!!!!!!!!!!!!!!!!!!!!!!!!!!!//
 function __construct ($name, $age, $sex){
  $this->name = $name;
  $this->age = $age;
  $this->sex = $sex;
 }
 function pay() {
  echo "
你的名字是:".$this->name."你的年龄:".$this->age."你的性别:".$this->sex."
";
 }
 //销毁
 /*function __destruct()
 {
  echo "
再见".$this->name;
 }
 */
}
字串6
$stu1 = new stu("stu1","F",20);
$stu2 = new stu("stu2","N",22);
$stu3 = new stu("stu3","F",24);
字串4
$stu1->pay();
$stu2->pay();
$stu3->pay(); 字串2
echo "
---------------------------------------------------
"; 字串4
/*
字串3
_set() __get() __isset() __unset()四个方法的应用
字串2
*/
字串9
class xx{
 private $name;
 private $age;
 private $sex;
 private function __get($property_name){
  if(isset($property_name))
   return ($this->$property_name);
  else
   return (NULL);
 }
 private function __set($property_name,$value){
  $this->$property_name = $value;
 }
 private function __isset($nm)
 {
  echo "isset()函数测定私有成员时,自动调用
";
  return isset($this->$nm);
 }
  //__unset()方法
 private function __unset($nm)
 {
  echo "当在类外部使用unset()函数来删除私有成员时自动调用的
";
  unset($this->$nm);
 }
} 字串3
$a = new xx();
$a->name  = "扬帆";
unset($a->name);
echo $a->name;
字串8
$p1 = new xx();
$p1->name = "this is a person name";
//在使用isset()函数测定私有成员时,自动调用__isset()方法帮我们完成,返回结果为true
echo var_dump(isset($p1->name))."
";
echo $p1->name."
";
//在使用unset()函数删除私有成员时,自动调用__unset()方法帮我们完成,删除name私有属性
unset($p1->name);
//已经被删除了, 所这行不会有输出
echo $p1->name;
?> 字串9

0

顶一下

0

踩一下
友情链接 | 诚聘英才 | 关于我们 | 版权声明 | 联系我们 | 广告服务
  •     大智学场copyright©2007 站长QQ:138290388 闽ICP备05004390号 
    声明:站内提供的所有教程文章均是由网上搜集或网友提供,若侵犯了你的版权利益,敬请来信通知我们!