php class

1. class 构造 、析构、成员函数 基础

<?php

class CTest{
const STD_TYPE = 1;
public $name;
public $age = 1;

public function __construct($name ,$age) {  //如果没有,会自动生成一个没有参数,没有动作的构造函数
  $this->name = $name;
  $this->age = $age;  
  echo "construct   name:$this->name \n";
}

public function set($name, $age, $type) {
  if ($type != self::STD_TYPE)
    return false;
  if ($age>100 && $age<1 && $name=='')
    return false;
  $this->name = $name;   //this 引用成员变量
  $this->age = $age;
  return true;
}

public function get() {
  return array('name' => $this->name, 'age' => $this->age);
}

public function __destruct() {
  echo "destruct   name:$this->name \n";
}
}


echo 'ctest::std_type:'.CTest::STD_TYPE."\n";
$test = new CTest();   //有warning
$ret = $test->set('hello kitty', 73, 1); 
echo 'ret:'.$ret."\n";
print_r ($test->get());

$test1 = new CTest(214, 'abc');
print_r ($test1->get());

?>

2.  类的继承

<?php
class BaseClass {
  public $name = 'name_base';
  public $age= 'age_base';
  public function __construct() {
    echo "construct   base\n";
    echo "base   $this->name   $this->age\n";
  }
  public function set($name , $age) {
    $this->name = $name;
    $this->age = $age;
  }
  public function get() {
    return $this->name;
  }

  public function create() {
    echo "base create name:".$this->get()."\n";
  }

  public function test() {
    echo "base test\n";
  }
}

class SuperClass extends BaseClass {
  public $name = 'name_super';
  public function __construct() {
    echo "construct   super\n";
    echo "super   $this->name   $this->age\n";
  }

  public function get() {
    return $this->name.'  super';
  }
}


class HeroClass extends BaseClass {
  public $name = 'name_hero';

  public function get() {
    return $this->name.'  hero';
  }

  public function test() {
    parent::test();  //显式调用基类的成员方法
    echo "hero test\n";
  }
}

$demo = 1;
if ($demo == 1) {
  $testBase = new  BaseClass;
  $testSuper = new SuperClass;
  $testHero = new  HeroClass;
} else if ($demo == 2) {
  $testHero = new  HeroClass;
  $testHero->set('abc', 123);  //给不存在的变量赋值

  $testHero->create()."\n";
} else if ($demo == 3) {
  $testHero = new  HeroClass();
  $testHero->test();
}


/*

1. 覆盖。如果子类存在同父类同名的成员变量或者函数,在声明子类对象时,则使用子类的成员,如果没有就用父类的。
base--> fun1  fun2
derived --> fun1 fun2
a.如果 derived对象调用fun1, 则调用derived类中申明的,如果没有调用base的fun1
b.如果 derived对象调用fun1, fun1调用成员函数 fun2.如果derived类中未申明fun1, 则调用derived类中申明的。如果base和 derived类中都声明了fun2,则基类的fun1会调用derived类的fun2。
所以,总结 子类对象调用 能用子类的就用子类的,不能用采用基类的
2. 如果子类有构造函数, 则不会调用基类的构造函数。 如果基本声明构造函数,子类没有显式声明,则子类会调用基类。
3. 成员变量 必须声明
4. create+get 完成一个典型的继承多态模式, 可以多用
*/
?>


3. 虚函数、接口

<?php
/* part1
继承*/
interface if1{
  function getnickname();
}

abstract class base {
  public $name = 'base_name';
  public abstract function getName();
  public function print_n() {
    return $this->getName()."   getname \n";
  }
}

class derived extends base implements if1  { //php 只支持单继承, 多继承需要用interface
  public $name = 'derived_name';
  public function getName() {
    return $this->name;
  }
  public function getnickname() {
    return $this->name."   nick";
  }
  public function __clone() {   //只在对象被clone时,进行调用
    $this->name = 'derived_clone_name';
  }
}

function demo1() {
  $test = new derived;
  //$test = new base;
  echo $test->print_n();
  echo $test->getnickname();
}
//demo1();
/*
1. 如果不想让基类也具有 print_n功能,设置 abstract 功能 (推荐)
*/
/////////////////////////////////////
/* part2: 对象赋值
*/
function demo2() {
  $test2 = new derived;
  //$test1 = $test2;  //赋值引用
  $test1 = clone $test2;   //复制对象,引用不同
  if ($test1 == $test2)    //比较内容
    echo "content equals";
  else
    echo "content not equals";

  if ($test1 === $test2)   //比较引用
    echo "reference equals";
  else
    echo "reference not equals";
}
demo2();


/////////////////////////////////////
/* part3: 对象赋值
*/
function demo3() {
  $test3 = new derived;
  if ($test3 instanceof derived)
    echo 'equals';
  else
    echo 'not equals';
}
//demo3();
?>
     


4. static 成员

<?php
final class base {  //本类不可被继承
  //public $name;
  public static $name;
  public $age;
  public function echos() {
    echo "hello world\n";
  }
  public function set($name, $age) {
    self::$name = $name;
    self::echos();
  }
  public static function set1($name, $age) {
    self::$name = $name;
//    $this->age = $age;
  }
  public function get() {
    echo 'name:'.self::$name;
  }
  
}

$demo = 2;
if ($demo == 1) {
  base::set('abc', 123);
  base::get();
} else if ($demo ==2) {
  $test = new base;
  $test->set1('abc', 123);
  $test->get();

}

/*
0. 静态成员函数,只能调用静态成员变量,不能调用非静态。非静态成员函数,可以调用静态 和 非静态变量和函数
1. 在1调用方式中,在static函数中不能调用成员变量 age、函数,因为 此时对象未生成,没有this。
2. 在用1调用方式时,静态函数不会检测是否有static关键字,如果函数中出现非静态变量或函数则报错
3. 在2中调用,有static的静态函数会显式的检查 是否调用非静态函数,如果有 则报错。

*/

?>


5.  magic functions

<?php
class CTest {
  public $name = ''; 
  public $temp = array();
  public function __set($name, $value) {   //当设置不存在的成员变量就会调用 
    echo "set undefined  name:$name   value:$value  \n";
    $this->temp[$name] = $value;
  }
  public function __get($name) { //当读取不存在的成员变量就会调用 
    echo "get undefined  name:$name   \n";
    return $this->temp[$name];
  }
  public function __call($method, $paras) { //当调用不存在的成员函数就会调用 
    echo "call undefined method:$method\n";
    var_dump($paras);
  }

  public function __toString() {   //直接输入对象时,将对象字符串化
    return 'tostring:'.$this->name;
  }
}

$test = new CTest;
$test->type = 1;
$test->name = 2;
echo $test->type."\n";

$test->set('1234', 3456);

echo "ctest:$test \n"
?>

6. autoload

<?php
class Person {
  private $name = 'default_name';
  private $age = 'default_age';

  public function set($name) {
    $this->name = $name;
  }
  public function get() {
    return $this->name;
  }
}
?>

<?php
//require_once('Person.class.php');
function __autoload($name) {  //在调用不存在的类时,自动调用autoload
  $className = "$name.class.php";
  if (file_exists($className)) {
    echo "class:$name  found\n";
    require_once($className);
  } else 
    echo "class:$name not  found\n";
    
}

$test = new Person;
$test->set('abcc');
echo $test->get()."\n";

?>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值