function __constrct([参数列表]){
方法体 //通常用来对成员属性进行初始化赋值
}
<?php
class Person
{
public $name;
public $age;
public $sex;
/**
* 显示声明一个构造方法且带参数
*/
public function __construct($name="", $sex="男", $age=22)
{
$this->name = $name;
$this->sex = $sex;
$this->age = $age;
}
/**
* say 方法
*/
public function say()
{
echo "我叫:" . $this->name . ",性别:" . $this->sex . ",年龄:" . $this->age;
}
}
$Person1 = new Person(); echo $Person1->say(); //输出:我叫:,性别:男,年龄:27
$Person2 = new Person("小明");
echo $Person2->say(); //输出:我叫:张三,性别:男,年龄:27
$Person3 = new Person("李四","男",25);
echo $Person3->say(); //输出:我叫:李四,性别:男,年龄:25
function __destruct()
{
//方法体
}
<?php
class Person{
public $name;
public $age;
public $sex;
public function __construct($name="", $sex="男", $age=22)
{
$this->name = $name;
$this->sex = $sex;
$this->age = $age;
}
/**
* say 说话方法
*/
public function say()
{
echo "我叫:".$this->name.",性别:".$this->sex.",年龄:".$this->age;
}
/**
* 声明一个析构方法
*/
public function __destruct()
{
echo "我觉得我还可以再抢救一下,我的名字叫".$this->name;
}
}
$Person = new Person("小明");
unset($Person); //销毁上面创建的对象$Person
function __call(string $function_name, array $arguments)
{
// 方法体
}
<?php
class Person
{
function say()
{
echo "Hello, world!<br>";
}
/**
* 声明此方法用来处理调用对象中不存在的方法
*/
function __call($funName, $arguments)
{
echo "你所调用的函数:" . $funName . "(参数:" ; // 输出调用不存在的方法名
print_r($arguments); // 输出调用不存在的方法时的参数列表
echo ")不存在!<br>\n"; // 结束换行
}
}
$Person = new Person();
$Person->run("teacher"); // 调用对象中不存在的方法,则自动调用了对象中的__call()方法
$Person->eat("小明", "苹果");
$Person->say();
<?php
class Person
{
function say()
{
echo "Hello, world!<br>";
}
/**
* 声明此方法用来处理调用对象中不存在的方法
*/
public static function __callStatic($funName, $arguments)
{
echo "你所调用的静态方法:" . $funName . "(参数:" ; // 输出调用不存在的方法名
print_r($arguments); // 输出调用不存在的方法时的参数列表
echo ")不存在!<br>\n"; // 结束换行
}
}
$Person = new Person();
$Person::run("teacher"); // 调用对象中不存在的方法,则自动调用了对象中的__call()方法
$Person::eat("小明", "苹果");
$Person->say();
<?php
class Person
{
private $name;
private $age;
function __construct($name="", $age=1)
{
$this->name = $name;
$this->age = $age;
}
/**
* 在类中添加__get()方法,在直接获取属性值时自动调用一次,以属性名作为参数传入并处理
* @param $propertyName
*
* @return int
*/
public function __get($propertyName)
{
if ($propertyName == "age") {
if ($this->age > 30) {
return $this->age - 10;
} else {
return $this->$propertyName;
}
} else {
return $this->$propertyName;
}
}
}
$Person = new Person("小明", 60); // 通过Person类实例化的对象,并通过构造方法为属性赋初值
echo "姓名:" . $Person->name . "<br>"; // 直接访问私有属性name,自动调用了__get()方法可以间接获取
echo "年龄:" . $Person->age . "<br>"; // 自动调用了__get()方法,根据对象本身的情况会返回不同的值
<?php
class Person
{
private $name;
private $age;
public function __construct($name="", $age=25)
{
$this->name = $name;
$this->age = $age;
}
/**
* 声明魔术方法需要两个参数,真接为私有属性赋值时自动调用,并可以屏蔽一些非法赋值
* @param $property
* @param $value
*/
public function __set($property, $value) {
if ($property=="age")
{
if ($value > 150 || $value < 0) {
return;
}
}
$this->$property = $value;
}
/**
* 在类中声明说话的方法,将所有的私有属性说出
*/
public function say(){
echo "我叫".$this->name.",今年".$this->age."岁了";
}
}
$Person=new Person("小明", 25); //注意,初始值将被下面所改变
//自动调用了__set()函数,将属性名name传给第一个参数,将属性值”李四”传给第二个参数
$Person->name = "小红"; //赋值成功。如果没有__set(),则出错。
//自动调用了__set()函数,将属性名age传给第一个参数,将属性值26传给第二个参数
$Person->age = 16; //赋值成功
$Person->age = 160; //160是一个非法值,赋值失效
$Person->say(); //输出:我叫小红,今年16岁了
<?php
class Person
{
public $sex;
private $name;
private $age;
public function __construct($name="", $age=25, $sex='男')
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
/**
* @param $content
*
* @return bool
*/
public function __isset($content) {
echo "当在类外部使用isset()函数测定私有成员{$content}时,自动调用<br>";
echo isset($this->$content);
}
}
$person = new Person("小明", 25); // 初始赋值
echo isset($person->sex),"<br>";
echo isset($person->name),"<br>";
echo isset($person->age),"<br>";
<?php
class Person
{
public $sex;
private $name;
private $age;
public function __construct($name="", $age=25, $sex='男')
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
/**
* @param $content
*
* @return bool
*/
public function __unset($content) {
echo "当在类外部使用unset()函数来删除私有成员时自动调用的<br>";
echo isset($this->$content);
}
}
$person = new Person("小明", 25); // 初始赋值
unset($person->sex);
unset($person->name);
unset($person->age);
<?php
class Person
{
public $sex;
public $name;
public $age;
public function __construct($name="", $age=25, $sex='男')
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
/**
* @return array
*/
public function __sleep() {
echo "当在类外部使用serialize()时会调用这里的__sleep()方法<br>";
$this->name = base64_encode($this->name);
return array('name', 'age'); // 这里必须返回一个数值,里边的元素表示返回的属性名称
}
}
$person = new Person('小明'); // 初始赋值
echo serialize($person);
echo '<br/>';
<?php
class Person
{
public $sex;
public $name;
public $age;
public function __construct($name="", $age=25, $sex='男')
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
/**
* @return array
*/
public function __sleep() {
echo "当在类外部使用serialize()时会调用这里的__sleep()方法<br>";
$this->name = base64_encode($this->name);
return array('name', 'age'); // 这里必须返回一个数值,里边的元素表示返回的属性名称
}
/**
* __wakeup
*/
public function __wakeup() {
echo "当在类外部使用unserialize()时会调用这里的__wakeup()方法<br>";
$this->name = 2;
$this->sex = '男';
// 这里不需要返回数组
}
}
$person = new Person('小明'); // 初始赋值
var_dump(serialize($person));
var_dump(unserialize(serialize($person)));
<?php
class Person
{
public $sex;
public $name;
public $age;
public function __construct($name="", $age=25, $sex='男')
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
public function __toString()
{
return 'go go go';
}
}
$person = new Person('小明'); // 初始赋值
echo $person;
<?php
class Person
{
public $sex;
public $name;
public $age;
public function __construct($name="", $age=25, $sex='男')
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
}
$person = new Person('小明'); // 初始赋值
echo $person;
<?php
class Person
{
public $sex;
public $name;
public $age;
public function __construct($name="", $age=25, $sex='男')
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
public function __invoke() {
echo '这可是一个对象哦';
}
}
$person = new Person('小明'); // 初始赋值
$person();
<?php
class Person
{
public $sex;
public $name;
public $age;
public function __construct($name="", $age=25, $sex='男')
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
}
$person = new Person('小明'); // 初始赋值
var_export($person);
<?php
class Person
{
public $sex;
public $name;
public $age;
public function __construct($name="", $age=25, $sex='男')
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
public static function __set_state($an_array)
{
$a = new Person();
$a->name = $an_array['name'];
return $a;
}
}
$person = new Person('小明'); // 初始赋值
$person->name = '小红';
var_export($person);
<?php
class Person
{
public $sex;
public $name;
public $age;
public function __construct($name="", $age=25, $sex='男')
{
$this->name = $name;
$this->age = $age;
$this->sex = $sex;
}
public function __clone()
{
echo __METHOD__."你正在克隆对象<br>";
}
}
$person = new Person('小明'); // 初始赋值
$person2 = clone $person;
var_dump('persion1:');
var_dump($person);
echo '<br>';
var_dump('persion2:');
var_dump($person2);
/**
* 文件non_autoload.php
*/
require_once('project/class/A.php');
require_once('project/class/B.php');
require_once('project/class/C.php');
if (条件A) {
$a = new A();
$b = new B();
$c = new C();
// … 业务逻辑
} else if (条件B) {
$a = newA();
$b = new B();
// … 业务逻辑
}
/**
* 文件autoload_demo.php
*/
function __autoload($className) {
$filePath = “project/class/{$className}.php”;
if (is_readable($filePath)) {
require($filePath);
}
}
if (条件A) {
$a = new A();
$b = new B();
$c = new C();
// … 业务逻辑
} else if (条件B) {
$a = newA();
$b = new B();
// … 业务逻辑
}
<?php
class C {
private $prop;
public function __construct($val) {
$this->prop = $val;
}
/**
* @return array
*/
public function __debugInfo() {
return [
'propSquared' => $this->prop ** 2,
];
}
}
var_dump(new C(42));
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有