Php面向对象学习笔记 ? 构造、析构、对象赋值、克隆
Php面向对象学习笔记 ? 构造、析构、对象赋值、克隆
class student
{
public $stu_id; // 定义成员变量
public $stu_name;
public function sayName() // 成员函数
{
echo $this->stu_name; // $this 访问该对象的成员变量
}
}
$stu = new Student; // 创建对象
$stu->stu_id = ‘0607001’;
$stu->stu_name = ‘小李’;
$stu->sayName();
注意:
class student
{
public $stu_id; // 定义成员变量
public $stu_name;
public function sayName() // 成员函数
{
var_dump($stu_name);
var_dump($GLOBALS[‘stu_name’]); // 两种方法都无法访问成员变量
//只要使用$this-> 才能访问成员变量
}
}
$stu = new Student; // 创建对象
$stu->stu_id = ‘0607001’;
$stu->stu_name = ‘小李’;
$stu->sayName();
注:类中定义的属性,不相当于类中定义的全局变量,不能直接再方法中使用属性变量的形式访问。
1. 构造和析构:
php的opp机制,在new完成时,会试着调用一个叫做__construct()的方法。
如果我们将初始化的代码,写到这个方法内,就可以完成自动初始化。
例子:
class Student
{
public $stu_id;
public $stu_name;
public function__construct($id,$name) // 构造
{
$this->stu_id= $id;
$this->stu_name= $name;
}
}
$stu = new Student(‘100511101’,’songyang’);
注:如果构造方法没有参数,则 $stu = new Student 和 $stu = new Student() 都是对的。
构造方法的兼容性问题:
php5,构造方法的名字,就是__construct().在php5之前,构造方法的名字为与类同名。为了兼容,也同时支持与类同名的构造方法。
如果同时出现__construct() 和类名的构造方法:
例子:
class Student
{
public $stu_id;
public $stu_name;
public function __construct()
{
echo“construct run …”;
}
public function Student()
{
echo‘’Student run …”;
}
{
$stu = new Student;
输出:construct run …
结论:如果同时出现两种构造方法,调用__construct()
析构:
在对象被销毁时,也会自动执行一个方法。
析构方法名字为:__destruct();
class Student
{
public $stu_id;
public $stu_name;
public function __construct()
{
echo“构造方法调用”;
}
//析构
publicfunction __destruct()
{
//释放资源
echo“析构方法调用”;
}
}
该方法,用于释放,该对象所占用的额外资源,不是对象本身的内存空间!
什么情况下,对象会被销毁:
1. 脚本周期结束,对象自动被销毁。
2. 销毁保存该对象的变量。
$stu = new Student;
unset($stu);
输出:析构方法调用
3. 保存对象的变量,被赋值了其他数据。
$stu = new Student;
$stu = “new Value”; // 赋值其他数据时,Student对象被销毁。
对象间的赋值
对象支持引用传递,不用&符号,因此不能通过=赋值的形式,得到一个新的对象。
例子
class Student
{
public $stu_id;
public $stu_name;
public function __construct($id,$name)
{
$this->stu_id= $id;
$this->stu_name= $name;
}
}
$stu1 = new Student(“100”,”song”);
$stu2 = $stu1;
echo $stu1->stu_name;
echo $stu2->stu_name;
$stu1->stu_name = “songyang”;
echo $stu1->stu_name;
echo $stu2->stu_name;
输出:song song songyang songyang
克隆
利用已有对象,得到相同的新对象。
需要使用关键字 clone
新对象 = clone 已有对象
例子:
class Student
{
public $stu_id;
public $stu_name;
public function __construct($id,$name)
{
$this->stu_id= $id;
$this->stu_name= $name;
}
}
$stu1 = new Student(“100”,”song”);
$stu2 = clone $stu1;
echo $stu1->stu_name;
echo $stu2->stu_name;
$stu1->stu_name = “songyang”;
echo $stu1->stu_name;
echo $stu2->stu_name;
输出:song song songyang song
常见的操作,再克隆对象时,需要对对象的某些特殊属性进行修改。意味着,需要做一些特殊的处理。
使用,在克隆时,自动调用的方法 __clone()来实现。
自动使用克隆出来的对象,来调用这个__clone()方法,意味着,该方法内部的$this,表示新对象。
例子:
class Student
{
public $stu_id;
public $stu_name;
public function __construct($id,$name)
{
$this->stu_id= $id;
$this->stu_name= $name;
}
public function __clone()
{
$this->stu_id= “0607002”;
}
}
$stu1 = new Student(“1000”,”joker”);
$stu2 = clone $stu1;
echo $stu1->stu_id;
echo $stu2->stu_id;
输出:1000 0607002

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
