时间:2021-07-01 10:21:17 帮助过:6人阅读
$name = $value;
}
public function __get($name){
if (!isset($this->$name)){
echo 'no set ';
$this->$name = 'set default value:';
}
return $this->$name;
}
}
$a = new Account();
echo $a -> user;
echo "
";
$a->user = 5;
echo $a->$name;
echo "
";
echo $a->big;若类中定义了__set和__get魔术方法,那么当给对象赋值或取值时,既使属性不存在也不会报错。增强程序的健壮性。fieldvalues[$fieldname];
}
static function __callStatic($method,$args){
$field = preg_replace('/^findBy(\w*)$','${1}',$method);
$query = "select * from "
.static::$table
." where $field='$args[0]";
return self::createDomain($query);
}
private static function createDomain($query){
echo "test";
$klass = get_called_class();
$domain = new $klass();
$domain->fieldvalues = array();
$domain->select = $query;
foreach($klass::$fields as $field => $type){
$domain->fieldvalues[$field] = 'TODO:set from sql result';
}
return $domain;
}
}
class Customer extends ActiveRecord{
protected static $table = 'custdb';
protected static $fields = array(
'id' => 'int',
'email' => 'varchar',
'lastname' => 'varchar'
);
public function __construct(){
echo "***";
}
}
class Sales extends ActiveRecord{
protected static $table = 'salesdb';
protected static $fields = array(
'id' => 'int',
'item' => 'varchar',
'qty' => 'int'
);
}
assert("select * from custdb where id = 123" == Customer::findById(123) ->select);
assert("TODO: set from sql result" == Customer::findById(123)->email);
assert("select * from salesdb where id = 321"== Sales::findById(321)->select);
assert("select * from custdb where lastname = 'Denoncourt'"== Customer::findByLastname('Denoncourt')->select);使用__call与__callStatic可以‘防止调用不存在的方法而报错’。还可以使得方法的动态创建变为可能。user},密码是{$this->pwd}";
}
}
$a = new Account();
echo $a;
echo "
";
echo PHP_EOL."===";
echo "
";
print_r($a);实际上,toString方法也是序列化的一种方式。name,"\tis",$this->gender,"\r\n";
}
}
class family extends person{
public $name;
public $gender;
public $age;
static $money = 100000;
public function __construct(){
parent::__construct();
echo "这里是子类",PHP_EOL;
}
public function say(){
echo "
我说".$this->name,"\tis\t",$this->gender,",and is \t",
$this->age,PHP_EOL."
";
}
public function cry(){
echo parent::$money,PHP_EOL;
echo "%>-<%",PHP_EOL;
echo self::$money,PHP_EOL;
echo "(*^_^*)";
}
public function read(){
echo "
read again".parent::say()."
";
}
}
$poor = new family();
$poor->name = 'Lee';
$poor->gender = 'female';
$poor->age = 25;
$poor->say();
$poor->cry();
$poor->read();
/*
* 疑问:
* 我在子类上加了个方法叫read
* 结果执行结果成了:
* 这里是父类 这里是子类
我说Lee is female,and is 25
10000 %>-<% 100000 (*^_^*)Lee isfemale
read again
为什么read again在lee is female后面……(我感觉应该先执行read again)
*/car = new car;
}
public function addoil(){
$this->car->addoil();
}
}
$bmw = new bmw();
$bmw ->addoil();
$benz = new benz();
$benz->addoil();使用继承的场景:working();
}
}
doprint(new teacher());
doprint(new coder());
doprint(new employee());
doprint(new readBooks());通过接口可以实现多态。 版权声明:本文为博主原创文章,未经博主允许不得转载。
以上就介绍了(二)PHP面向对象理论2,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。