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

PHPidentifiesauser'ssessionusingsessioncookiesandsessionIDs.1)Whensession_start()iscalled,PHPgeneratesauniquesessionIDstoredinacookienamedPHPSESSIDontheuser'sbrowser.2)ThisIDallowsPHPtoretrievesessiondatafromtheserver.

The security of PHP sessions can be achieved through the following measures: 1. Use session_regenerate_id() to regenerate the session ID when the user logs in or is an important operation. 2. Encrypt the transmission session ID through the HTTPS protocol. 3. Use session_save_path() to specify the secure directory to store session data and set permissions correctly.

PHPsessionfilesarestoredinthedirectoryspecifiedbysession.save_path,typically/tmponUnix-likesystemsorC:\Windows\TemponWindows.Tocustomizethis:1)Usesession_save_path()tosetacustomdirectory,ensuringit'swritable;2)Verifythecustomdirectoryexistsandiswrita

ToretrievedatafromaPHPsession,startthesessionwithsession_start()andaccessvariablesinthe$_SESSIONarray.Forexample:1)Startthesession:session_start().2)Retrievedata:$username=$_SESSION['username'];echo"Welcome,".$username;.Sessionsareserver-si

The steps to build an efficient shopping cart system using sessions include: 1) Understand the definition and function of the session. The session is a server-side storage mechanism used to maintain user status across requests; 2) Implement basic session management, such as adding products to the shopping cart; 3) Expand to advanced usage, supporting product quantity management and deletion; 4) Optimize performance and security, by persisting session data and using secure session identifiers.

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.


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

WebStorm Mac version
Useful JavaScript development tools

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
