php七天入门教程第七天

来源:互联网 发布:java接口的修饰符 编辑:程序博客网 时间:2024/04/30 11:11

/*

php七天入门教程分为以下一些内容:
第一天:
知识要点:1、php介绍及运行环境 2、php语言特点 3、简单的php语句 4、在html中嵌入php
第二天:
知识要点:1、变量 2、访问表单 3、使用操作符
第三天:
知识要点:1、一些常用、简单的函数 2、条件判断语句 3、一些关键的单词
第四天:
知识要点:文件处理
第五天:
知识要点:使用数组
第六天:
知识要点:1、正则表达式 2、php中的函数调用
第七天:
知识要点:1、面向对象的php 2、异常处理
------------------------------------------------- 字串4

面向对象的php 字串7

<?php 字串4

/*

字串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 "<br>名字:".$this->name."年龄:".$this->age."性别:".$this->sex."<br>";
 }
 //销毁
 
}
//定义学生
$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."<br>";
}
}
//通过构造方法创建3个对象$p1、p2、$p3,分别传入三个不同的实参为姓名、性别和年龄
$p1=new Person("张三","男", 20);
$p2=new Person("李四","女", 30);
$p3=new Person("王五","男", 40); 字串2

echo $p1->name."<br>";
echo $p1->sex."<br>";
echo $p1->age."<br>"; 字串9

  字串5

echo "<br>-----------------------------------------------------<br>"; 字串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 "<br>你的名字是:".$this->name."你的年龄:".$this->age."你的性别:".$this->sex."<br>";
 }
 //销毁
 /*function __destruct()
 {
  echo "<br>再见".$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 "<br>---------------------------------------------------<br>"; 字串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()函数测定私有成员时,自动调用<br>";
  return isset($this->$nm);
 }
  //__unset()方法
 private function __unset($nm)
 {
  echo "当在类外部使用unset()函数来删除私有成员时自动调用的<br>";
  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))."<br>";
echo $p1->name."<br>";
//在使用unset()函数删除私有成员时,自动调用__unset()方法帮我们完成,删除name私有属性
unset($p1->name);
//已经被删除了, 所这行不会有输出
echo $p1->name;
?> 字串9

 

本篇文章来源于中国教程网(www.cnjiaocheng.com)教程地址:http://www.cnjiaocheng.com/phprumen/20080316/233.html 

原创粉丝点击