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

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

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,

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.

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

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.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
