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

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

May 27, 2016 am 10:19 AM

这篇文章主要介绍了PHP动态地创建属性和方法, 对象的复制, 对象的比较, 加载指定的文件, 自动加载类文件, 命名空间 的相关资料,需要的朋友可以参考下

PHP前言:

•动态地创建属性和方法

•对象的复制

•对象的比较

•加载指定的文件

•自动加载类文件

•命名空间

示例

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

class/class3.php

<?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(&#39;, &#39;, $arguments);
echo "<br />";
}
// __callStatic 魔术方法,当调用的类方法不存在或者不可访问(private)时就会调用此函数
public static function __callStatic($name, $arguments)
{
echo "__callStatic \$name: {$name}, \$arguments: " . implode(&#39;, &#39;, $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

<?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 &#39;$objClass1 == $objClass2&#39;;
echo "<br />";
}
// 如果两个对象的属性和属性值都相等,但不是同一个类的实例,则他们“===”不相等
if ($objClass1 !== $objClass2)
{
echo &#39;$objClass1 !== $objClass2&#39;;
echo "<br />";
}
// 如果两个对象是同一个类的实例,则他们“===”相等
if ($objClass1 === $objClass1)
{
echo &#39;$objClass1 === $objClass1&#39;;
echo "<br />";
}
// 如果两个对象是同一个类的实例,则他们“===”相等
$objClass3 = &$objClass1;
if ($objClass1 === $objClass3)
{
echo &#39;$objClass1 === $objClass3&#39;;
echo "<br />";
}
Copy after login

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

class/class5.php

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

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

class/class6.php

<?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动态地创建属性和方法, 对象的复制, 对象的比较, 加载指定的文件, 自动加载类文件, 命名空间 的相关介绍,希望对大家有所帮助!

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

cURL in PHP: How to Use the PHP cURL Extension in REST APIs cURL in PHP: How to Use the PHP cURL Extension in REST APIs Mar 14, 2025 am 11:42 AM

The PHP Client URL (cURL) extension is a powerful tool for developers, enabling seamless interaction with remote servers and REST APIs. By leveraging libcurl, a well-respected multi-protocol file transfer library, PHP cURL facilitates efficient execution of various network protocols, including HTTP, HTTPS, and FTP. This extension offers granular control over HTTP requests, supports multiple concurrent operations, and provides built-in security features.

12 Best PHP Chat Scripts on CodeCanyon 12 Best PHP Chat Scripts on CodeCanyon Mar 13, 2025 pm 12:08 PM

Do you want to provide real-time, instant solutions to your customers' most pressing problems? Live chat lets you have real-time conversations with customers and resolve their problems instantly. It allows you to provide faster service to your custom

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

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,

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

See all articles