PHP5权威编程阅读学习笔记 附电子书下载
PHP 5 权威编程(PHP 5 Power Programming) PDF下载地址 http://www.jb51.net/books/28207.html
PHP4中,不使用__construct()作为构造函数的名字,必须使用类的名字定义一个方法,就像在C++中一样。
PHP5中,使用新的统一的构造函数命名方式:__construct(),当然,使用类名同样也是可以的。
但是,你如果两个同时使用的话,系统默认会使用__construct()的形式。
复制代码 代码如下:
class Person{
//PHP4中的方法
public function Person(){
echo "PHP4中的方法";
}
//PHP5推荐使用的方法
public function __construct(){
echo "PHP5推荐使用的方法";
}
public function say(){
}
}
$p1=new Person();
?>
在构造函数中不能返回值,所以从构造函数内产生一个错误最常用的做法就是抛出一个异常。
代码如下:
复制代码 代码如下:
class Person{
private $_age;
public function __construct($age){
try {
if ($age$this->_age=$age;
}else {
throw new Exception("您输入的年龄过大");
}
}catch (Exception $e){
echo $e->getMessage();
}
}
}
$p1=new Person(121);
?>
访问控制
对象属性的访问保护是OOP的一个关键范例
Public:可以在任何地方被访问
Protected:类成员可以被其所在类的子类和父类从对象内部的方法访问
Private:类成员只能被其所在类从对象内部的方法访问,而无法从继承类的成员中访问到。因为私用成员不会被继承,所以两个相关的类完全可以分别声明一个名字相同的私有变量。
也就是两个类都只能看到自己的私有属性,私有成员之间是没有关系的。
例子:
复制代码 代码如下:
/**
* Define MyClass
*/
class MyClass
{
public $public = 'Public';
protected $protected = 'Protected';
private $private = 'Private';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj = new MyClass();
echo $obj->public; // 这行能被正常执行
echo $obj->protected; // 这行会产生一个致命错误
echo $obj->private; // 这行也会产生一个致命错误
$obj->printHello(); // 输出 Public、Protected 和 Private
/**
* Define MyClass2
*/
class MyClass2 extends MyClass
{
// 可以对 public 和 protected 进行重定义,但 private 而不能
protected $protected = 'Protected2';
function printHello()
{
echo $this->public;
echo $this->protected;
echo $this->private;
}
}
$obj2 = new MyClass2();
echo $obj->public; // 这行能被正常执行
echo $obj2->private; // 未定义 private
echo $obj2->protected; // 这行会产生一个致命错误
$obj2->printHello(); // 输出 Public、Protected2,但不会输出 Private
注意:类中的方法都必须使用关键字public、protected 或 private 进行定义。如果没有设置这些关键字,则该方法会被设置成默认的 public。
静态方法
静态方法可以不创建对象实例就通过 类名::静态方法 来调用,也可以在一个对象实例中通过$this->静态方法或self::静态方法来调用。
复制代码 代码如下:
class Foo
{
public static $my_static = 'foo';
public static function staticValue() {
return self::$my_static;//在类中访问静态成员使用self关键字
}
}
$obj=new Foo();
echo $obj->staticValue();//方式一
echo Foo::staticValue();//方式二
?>
克隆对象
在PHP4中,new一个对象的时候,返回的是“对象本身”
在PHP5中,new一个对象的时候,返回的是“指向对象的句柄”
这意味着在PHP5中,将对象实例($obj1)赋值给另一个变量($obj2)的时候,两个对象都是指向同一块内存区域。
比如 :
复制代码 代码如下:
class test{
public $str;
}
$obj1=new test();
$obj1->str="obj1";
$obj2= $obj1;
$obj2->str="obj2";
echo $obj1->str;//将输出“obj1”
?>
由于$obj1和$obj2指向的是同一块内存区域,因此使用任何一个对象修改其中的成员变量的值的时候,都会影响到另一个对象。
但是在有些时候,我们确实需要复制一个对象的拷贝(两块相互独立的内存区域),这时候可以使用语言命令clone
参考下面的例子;
复制代码 代码如下:
class test{
public $str;
}
$obj1=new test();
$obj1->str="obj1";
$obj2= clone $obj1;
$obj2->str="obj2";
echo $obj1->str;//将输出“obj2”
?>
parent::和self::
self::指向当前类,而且通常用来访问静态成员,方法和常量
parent::指向父类,而且它经常被用来调用父类的构造函数和方法,也可以用来访问父类的成员和常量
注意:你应该使用parent::而不是父类的某个具体的名字,因为这样可以令你方便的更改你的类的层次。
例子:
复制代码 代码如下:
class Father{
public function __construct(){
echo "调用父类的构造函数
";
}
}
class Son extends Father {
public function __construct(){
parent::__construct();//方式一
// Father::__construct();//方式二
echo "调用子类的构造函数";
}
}
$son=new Son();
?>
结果:
调用父类的构造函数
调用子类的构造函数
推荐使用方式一,原因上面已经说了。
instanceof实例
复制代码 代码如下:
class Rectangle {
public $name=__CLASS__;
}
class Square extends Rectangle {
public $name=__CLASS__;
}
class Circle{
public $name=__CLASS__;
}
function checkIfRectangle($shape){
if ($shape instanceof Rectangle ){
echo $shape->name;
}else {
echo "该对象不是Rectangle类的实例";
}
}
checkIfRectangle(new Square());//输出:Square
checkIfRectangle(new Circle());//输出:该对象不是Rectangle类的实例
?>
注:__CLASS__是一个特殊的常量,用来存储当前类的名字

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

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

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

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

Python is an ideal programming introduction language for beginners through its ease of learning and powerful features. Its basics include: Variables: used to store data (numbers, strings, lists, etc.). Data type: Defines the type of data in the variable (integer, floating point, etc.). Operators: used for mathematical operations and comparisons. Control flow: Control the flow of code execution (conditional statements, loops).

Java Made Simple: A Beginner's Guide to Programming Power Introduction Java is a powerful programming language used in everything from mobile applications to enterprise-level systems. For beginners, Java's syntax is simple and easy to understand, making it an ideal choice for learning programming. Basic Syntax Java uses a class-based object-oriented programming paradigm. Classes are templates that organize related data and behavior together. Here is a simple Java class example: publicclassPerson{privateStringname;privateintage;

Java is a popular programming language that can be learned by both beginners and experienced developers. This tutorial starts with basic concepts and progresses through advanced topics. After installing the Java Development Kit, you can practice programming by creating a simple "Hello, World!" program. After you understand the code, use the command prompt to compile and run the program, and "Hello, World!" will be output on the console. Learning Java starts your programming journey, and as your mastery deepens, you can create more complex applications.

Pythonempowersbeginnersinproblem-solving.Itsuser-friendlysyntax,extensivelibrary,andfeaturessuchasvariables,conditionalstatements,andloopsenableefficientcodedevelopment.Frommanagingdatatocontrollingprogramflowandperformingrepetitivetasks,Pythonprovid
