PHP高级特征基础
PHP高级特性基础
php对象在内存中的区域:php对象名和对象存放的位置是不一样的,这一点和java是一模一样的,所以在php面向对象部分你完全可以套用java的思想去做。对象存放在堆区(heap)而对象名则和其他普通变量一样存放在栈里面。注意对象名本身是一个指向对象的引用,也就是它的值其实是它所指向的对象的地址,所以你可以更改它指向的对象来使它指向完全不同的对象,同理,一个对象可以有多个对象名,但要注意,通过修改其中的一个对象名的成员属性就会导致其他对象名所指向的成员属性也发生变化。
基本类型变量和对象名是存放在桟区的,而对象是存放在堆区的。然后对象名是引用,也就是值是地址,而基本类型变量只有添加&地址符才表示地址。注意this关键字的运用,特别是区分类的成员变量和成员函数里的局部变量。
php的构造方法:
在php5中新添加的构造方法形式为function __construct($num1);而在php4中构造方法是以类名为构造方法名,这一点类似于java。可以写两个构造方法,但执行的时候只会选择其中一个,建议用php5的构造方法形式。在成员方法中如果直接写变量,而没有在变量前加上$this->那么这个变量就是一个活跃在方法内的局部变量,而在方法结束了这个变量就释放了。所以要在方法里面使用,一定要记得this关键字。注意不能重载默认构造方法,即不能这样写function __construct()这个方法是默认的构造方法。一个类只能有一个构造方法。但版本不同的构造方法可以共存。
而且创建对象和你写的构造方法一定要相对应。
php的析构方法:
形式:function __destruct(){
//释放资源操作
}
类似于C++,作用是用于释放资源,回收变量所占用的内存资源,销毁某个对象。默认的销毁对象顺序和创建对象顺序相反,这一点和栈相关,越晚被创建,越早被销毁。创建对象是入栈,销毁对象操作时出栈。
注意将对象置NULL时是为了销毁对象,而这个对象有多个变量指向这个对象,那么这个对象并不会被销毁。例如
$pig =new Pig();
$pig2=$pig;
$pig=null;
该对象并没有被置空,因为还有一个变量指向这个对象。
static和global变量都是存放在”全局区/静态数据区”。访问静态变量用self::关键字前缀。其实也可以其他类的静态方法,形式为:
类名::静态方法名字,访问父类的构造方法或静态方法用parent::方法名 形式;
在php中静态方法只能操作静态变量,不能操作成员变量,而成员方法既可以操作静态变量也可以操作成员变量。
php函数只允许覆盖,不允许重载。即不能有同名函数。
unset一个引用变量时,unset销毁的是指向对象的变量,而不是这个对象。
成员方法默认是public的,而成员属性默认是private的。
php中多态的实现;
php中多态的实现过程和其他高级语言不一样,它是通过魔术函数__call()实现的;该函数用在系统调用一个对象的方法时该方法名不存在,就会调用__call()函数,多态不建议使用。

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

AI Hentai Generator
Generate AI Hentai for free.

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

In this chapter, we will understand the Environment Variables, General Configuration, Database Configuration and Email Configuration in CakePHP.

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

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

To work on file upload we are going to use the form helper. Here, is an example for file upload.

In this chapter, we are going to learn the following topics related to routing ?

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

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

Validator can be created by adding the following two lines in the controller.
