Table of Contents
PHP动态地创建属性和方法, 对象的复制, 对象的比较,加载指定的文件,自动加载类文件,命名空间,
Home Backend Development PHP Tutorial PHP dynamically creates properties and methods, copies objects, compares objects, loads specified files, automatically loads class files, namespaces, _PHP tutorial

PHP dynamically creates properties and methods, copies objects, compares objects, loads specified files, automatically loads class files, namespaces, _PHP tutorial

Jul 12, 2016 am 08:53 AM
php Attributes method

PHP动态地创建属性和方法, 对象的复制, 对象的比较,加载指定的文件,自动加载类文件,命名空间,

PHP前言:

•动态地创建属性和方法

•对象的复制

•对象的比较

•加载指定的文件

•自动加载类文件

•命名空间

示例

1、类的相关知识点 3(动态地创建属性和方法)

class/class3.php

<&#63;php
/**
* 类的相关知识点 3(动态地创建属性和方法)
*/
// 用于演示如何动态地创建属性(这就是 php 中所谓的重载)
class Class1
{
// __set 魔术方法,当设置的属性不存在或者不可访问(private)时就会调用此函数
public function __set($name, $value)
{
echo "__set \$name: {$name}, \$value: {$value}";
echo "<br />";
}
// __get 魔术方法,当获取的属性不存在或者不可访问(private)时就会调用此函数
public function __get($name)
{
echo "__get \$name: {$name}";
echo "<br />";
return 999;
}
}
$objClass1 = new Class1();
// 当你设置的属性不存在或者不可访问(private)时,就会调用对应的 __set 魔术方法
$objClass1->property1 = wanglei; // 不可访问的如 private ,或者不存在的
// 当你获取的属性不存在或者不可访问(private)时,就会调用对应的 __get 魔术方法
echo $objClass1->property2;
echo "<br />";
// 用于演示如何动态地创建方法(这就是 php 中所谓的重载)
class Class2
{
// __call 魔术方法,当调用的实例方法不存在或者不可访问(private)时就会调用此函数
public function __call($name, $arguments)
{
echo "__call \$name: {$name}, \$arguments: " . implode(', ', $arguments);
echo "<br />";
}
// __callStatic 魔术方法,当调用的类方法不存在或者不可访问(private)时就会调用此函数
public static function __callStatic($name, $arguments)
{
echo "__callStatic \$name: {$name}, \$arguments: " . implode(', ', $arguments);
echo "<br />";
}
}
$objClass2 = new Class2();
// 当你调用的实例方法不存在或者不可访问(private)时,就会调用对应的 __call 魔术方法
echo $objClass2->method1("aaa", "bbb");
// 当你调用的类方法不存在或者不可访问(private)时,就会调用对应的 __callStatic 魔术方法
echo Class2::method2("aaa", "bbb"); 
Copy after login

2、类的相关知识点 4(对象的复制,对象的比较)

class/class4.php

<&#63;php
/**
* 类的相关知识点 4(对象的复制,对象的比较)
*/
// 用于演示如何复制对象
class Class1
{
public $field1 = "field1";
public $field2 = "field2";
// 通过 clone 复制对象时,会调用此魔术方法
function __clone()
{
echo "__clone";
echo "<br />";
}
}
$objClass1 = new Class1();
// 通过 clone 复制对象,会调用 __clone 魔术方法
$objClass2 = clone $objClass1;
// 通过 clone 复制的对象为浅拷贝(shallow copy),即成员数据之间的一一赋值, 而所有的引用属性仍然会是一个指向原来的变量的引用(如果要做 deep copy 则需要自己写)
echo $objClass2->field1; // output: field1
echo "<br />";
echo $objClass2->field2; // output: field2
echo "<br />";
// 如果两个对象的属性和属性值都相等,则他们“==”相等,
if ($objClass1 == $objClass2)
{
echo '$objClass1 == $objClass2';
echo "<br />";
}
// 如果两个对象的属性和属性值都相等,但不是同一个类的实例,则他们“===”不相等
if ($objClass1 !== $objClass2)
{
echo '$objClass1 !== $objClass2';
echo "<br />";
}
// 如果两个对象是同一个类的实例,则他们“===”相等
if ($objClass1 === $objClass1)
{
echo '$objClass1 === $objClass1';
echo "<br />";
}
// 如果两个对象是同一个类的实例,则他们“===”相等
$objClass3 = &$objClass1;
if ($objClass1 === $objClass3)
{
echo '$objClass1 === $objClass3';
echo "<br />";
} 
Copy after login

3、类的相关知识点 5(加载指定的文件,自动加载类文件)

class/class5.php

<&#63;php
/**
* 类的相关知识点 5(加载指定的文件,自动加载类文件)
*/
/*
* 包含并运行指定文件,可以是绝对路径也可以是相对路径
* include 找不到的话则警告,然后继续运行(include_once: 在当前文件中只 include 指定文件一次)
* require 找不到的话则错误,然后终止运行(require_once: 在当前文件中只 require 指定文件一次)
* include '';
* require '';
* include_once '';
* require_once '';
*/
// 演示如何通过 __autoload 魔术方法,来实现类的自动加载
function __autoload($class_name)
{
// 加载指定的文件
require_once $class_name . '.class.php';
}
// 如果在当前文件中找不到 MyClass 类,那么就会去调用 __autoload 魔术方法
$obj = new MyClass();
echo $obj->name;
echo "<br />"; 
class/MyClass.class.php
<&#63;php
class MyClass
{
public $name = "webabcd";
}
Copy after login

4、类的相关知识点 6(命名空间)

class/class6.php

<&#63;php
/**
* 类的相关知识点 6(命名空间)
*/
// 以下代码仅用于演示,实际项目中不建议在一个文件中定义多个 namespace
// 如果当前文件中只有一个命名空间,那么下面的这段可以省略掉命名空间的大括号,直接 namespace MyNamespace1; 即可
namespace MyNamespace1
{
const MyConst = "MyNamespace1 MyConst";
function myFunction()
{
echo "MyNamespace1 myFunction";
echo "<br />";
}
class MyClass
{
public function myMethod()
{
echo "MyNamespace1 MyClass myMethod";
echo "<br />";
}
}
}
// 定义命名空间时,可以指定路径
namespace Sub1\Sub2\MyNamespace2
{
const MyConst = "MyNamespace2 MyConst";
function myFunction()
{
echo "MyNamespace2 myFunction";
echo "<br />";
}
class MyClass
{
public function myMethod()
{
echo "MyNamespace2 MyClass myMethod";
echo "<br />";
}
}
}
namespace MyNamespace3
{
// 调用指定命名空间中的指定常量
echo \MyNamespace1\MyConst;
echo "<br />";
// 调用指定命名空间中的指定函数
\MyNamespace1\myFunction();
// 实例化指定命名空间中的类
$obj1 = new \MyNamespace1\MyClass();
$obj1->myMethod();
}
namespace MyNamespace4
{
// use 指定的命名空间
use \Sub1\Sub2\MyNamespace2;
// 之后不用再写全命名空间的路径了,因为之前 use 过了
echo MyNamespace2\MyConst;
echo "<br />";
MyNamespace2\myFunction();
$obj1 = new MyNamespace2\MyClass();
$obj1->myMethod();
}
namespace MyNamespace5
{
// use 指定的命名空间,并为其设置别名
use \Sub1\Sub2\MyNamespace2 as xxx;
// 之后再调用命名空间时,可以使用其别名
echo xxx\MyConst;
echo "<br />";
xxx\myFunction();
$obj1 = new xxx\MyClass();
$obj1->myMethod();
}
Copy after login

以上所述是小编给大家介绍的PHP动态地创建属性和方法, 对象的复制, 对象的比较, 加载指定的文件, 自动加载类文件, 命名空间 的相关介绍,希望对大家有所帮助!

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1123793.htmlTechArticlePHP动态地创建属性和方法, 对象的复制, 对象的比较,加载指定的文件,自动加载类文件,命名空间, PHP前言: 动态地创建属性和方法 对象的复...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

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

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian PHP 8.4 Installation and Upgrade guide for Ubuntu and Debian Dec 24, 2024 pm 04:42 PM

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

7 PHP Functions I Regret I Didn't Know Before 7 PHP Functions I Regret I Didn't Know Before Nov 13, 2024 am 09:42 AM

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

How To Set Up Visual Studio Code (VS Code) for PHP Development How To Set Up Visual Studio Code (VS Code) for PHP Development Dec 20, 2024 am 11:31 AM

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

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

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,

PHP Program to Count Vowels in a String PHP Program to Count Vowels in a String Feb 07, 2025 pm 12:12 PM

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

How do you parse and process HTML/XML in PHP? How do you parse and process HTML/XML in PHP? Feb 07, 2025 am 11:57 AM

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

Explain late static binding in PHP (static::). Explain late static binding in PHP (static::). Apr 03, 2025 am 12:04 AM

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 PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? What are PHP magic methods (__construct, __destruct, __call, __get, __set, etc.) and provide use cases? Apr 03, 2025 am 12:03 AM

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.

See all articles