PHP类和对象函数实例详解
1. interface_exists、class_exists、method_exists和property_exists:
顾名思义,从以上几个函数的命名便可以猜出几分他们的功能。我想这也是我随着对PHP的深入学习而越来越喜欢这门编程语言的原因了吧。下面先给出他们的原型声明和简短说明,更多的还是直接看例子代码吧。
bool interface_exists (string $interface_name [, bool $autoload = true ]) 判断接口是否存在,第二个参数表示在查找时是否执行__autoload。
bool class_exists (string $class_name [, bool $autoload = true ]) 判断类是否存在,第二个参数表示在查找时是否执行__autoload。
bool method_exists (mixed $object , string $method_name) 判断指定类或者对象中是否含有指定的成员函数。
bool property_exists (mixed $class , string $property) 判断指定类或者对象中是否含有指定的成员变量。
<span style="color: #000000;">php </span><strong><span style="color: #008000;">//</span></strong><span style="color: #008000;"><strong>in another_test_class.php</strong></span> <span style="color: #0000ff;">interface</span><span style="color: #000000;"> AnotherTestInterface { } </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> AnotherTestClass { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> printMe() { </span><span style="color: #0000ff;">print</span> "This is Test2::printSelf.\n"<span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> doSomething() { </span><span style="color: #0000ff;">print</span> "This is Test2::doSomething.\n"<span style="color: #000000;">; } </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span> doSomethingWithArgs(<span style="color: #800080;">$arg1</span>, <span style="color: #800080;">$arg2</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">print</span> 'This is Test2::doSomethingWithArgs with ($arg1 = '.<span style="color: #800080;">$arg1</span>.' and $arg2 = '.<span style="color: #800080;">$arg2</span>.").\n"<span style="color: #000000;">; } } </span><span style="color: #000000;">php </span><strong><span style="color: #008000;">//</span></strong><span style="color: #008000;"><strong>in class_exist_test.php,</strong> 下面测试代码中所需的类和接口位于another_test_class.php, //由此可以发现规律,类和接口的名称是驼峰风格的,而文件名的单词间是下划线分隔的。 //这里给出了两种__autoload的方式,因为第一种更为常用和方便,因此我们这里将第二种方式注释掉了,他们之间的差别可以查看manual。</span> <span style="color: #0000ff;">function</span> __autoload(<span style="color: #800080;">$classname</span><span style="color: #000000;">) { </span><span style="color: #800080;">$nomilizedClassname</span> = <span style="color: #008080;">strtolower</span>(<span style="color: #008080;">preg_replace</span>('/([A-Z]\w*)([A-Z]\w*)([A-Z]\w*)/','${1}_${2}_${3}',<span style="color: #800080;">$classname</span><span style="color: #000000;">)); </span><span style="color: #0000ff;">require</span> <span style="color: #008080;">strtolower</span>(<span style="color: #800080;">$nomilizedClassname</span>).".php"<span style="color: #000000;">; } </span><span style="color: #008000;">//</span><span style="color: #008000;">spl_autoload_register(function($classname) { // $nomilizedClassname = strtolower(preg_replace('/([A-Z]\w*)([A-Z]\w*)([A-Z]\w*)/','${1}_${2}_${3}',$classname)); // require strtolower($nomilizedClassname).".php"; //});</span> <span style="color: #0000ff;">print</span> "The following case is tested before executing autoload.\n"<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">class_exists</span>('AnotherTestClass',<span style="color: #0000ff;">false</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">print</span> "This class doesn't exist if no autoload.\n"<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (!<span style="color: #008080;">interface_exists</span>('AnotherTestInterface',<span style="color: #0000ff;">false</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">print</span> "This interface doesn't exist if no autoload.\n"<span style="color: #000000;">; } </span><span style="color: #0000ff;">print</span> "\nThe following case is tested after executing autoload.\n"<span style="color: #000000;">; </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">class_exists</span>('AnotherTestClass',<span style="color: #0000ff;">true</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">print</span> "This class exists if autoload is set to true.\n"<span style="color: #000000;">; } </span><span style="color: #0000ff;">if</span> (<span style="color: #008080;">interface_exists</span>('AnotherTestInterface',<span style="color: #0000ff;">true</span><span style="color: #000000;">)) { </span><span style="color: #0000ff;">print</span> "This interface exists if autoload is set to true.\n"<span style="color: #000000;">; }</span>
运行结果如下:
<span style="color: #000000;">bogon:TestPhp$ php class_exist_test.php The following </span><span style="color: #000000;">case</span><span style="color: #000000;"> is tested before executing autoload. This class doesn</span><span style="color: #800000;">'</span><span style="color: #800000;">t exist if no autoload.</span> This interface doesn<span style="color: #800000;">'</span><span style="color: #800000;">t exist if no autoload.</span> <span style="color: #000000;"> The following </span><span style="color: #000000;">case</span><span style="color: #000000;"> is tested after executing autoload. </span><span style="color: #000000;">This class exists if autoload is set to true. This interface exists if autoload is set to true.</span>
2. get_declared_classes和get_declared_interfaces:
分别返回当前可以访问的所有类和接口,这不仅包括自定义类和接口,也包括了PHP内置类和接口。他们的函数声明非常简单,没有参数,只是返回数组。见如下代码:
<span style="color: #000000;">php </span><span style="color: #0000ff;">interface</span><span style="color: #000000;"> AnotherTestInterface { } </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> AnotherTestClass { </span><span style="color: #0000ff;">public</span> <span style="color: #0000ff;">static</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> printMe() { </span><span style="color: #0000ff;">print</span> "This is Test2::printSelf.\n"<span style="color: #000000;">; } } </span><span style="color: #008080;">print_r</span>(<span style="color: #008080;">get_declared_interfaces</span><span style="color: #000000;">()); </span><span style="color: #008080;">print_r</span>(<span style="color: #008080;">get_declared_classes</span>());
由于输出结果过长,而且这两个函数也比较简单,所以下面就不再给出输出结果了。
3. get_class_methods、get_class_vars和get_object_vars:
这三个函数有一个共同点,即只能获取作用域可见范围内的所有成员函数、成员变量或非静态成员变量。比如在类的内部调用,则所有成员函数或者变量都符合条件,而在类的外部,则只有共有的函数和变量可以返回。
array get_class_methods (mixed $class_name) 获取指定类中可访问的成员函数。
array get_class_vars (string $class_name) 获取指定类中可以访问的成员变量。
array get_object_vars (object $object) 获取可以访问的非静态成员变量。
<span style="color: #000000;">php </span><span style="color: #0000ff;">function</span> output_array(<span style="color: #800080;">$functionName</span>, <span style="color: #800080;">$items</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">print</span> "<span style="color: #800080;">$functionName</span>.....................\n"<span style="color: #000000;">; </span><span style="color: #0000ff;">foreach</span> (<span style="color: #800080;">$items</span> <span style="color: #0000ff;">as</span> <span style="color: #800080;">$key</span> => <span style="color: #800080;">$value</span><span style="color: #000000;">) { </span><span style="color: #0000ff;">print</span> '$key = '.<span style="color: #800080;">$key</span>. ' => $value = '.<span style="color: #800080;">$value</span>."\n"<span style="color: #000000;">; } } </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> TestClass { </span><span style="color: #0000ff;">public</span> <span style="color: #800080;">$publicVar</span> = 1<span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #800080;">$privateVar</span> = 2<span style="color: #000000;">; </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">private</span> <span style="color: #800080;">$staticPrivateVar</span> = "hello"<span style="color: #000000;">; </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">public</span> <span style="color: #800080;">$staticPublicVar</span><span style="color: #000000;">; </span><span style="color: #0000ff;">private</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> privateFunction() { } </span><span style="color: #0000ff;">function</span><span style="color: #000000;"> publicFunction() { output_array(</span>"get_class_methods",<span style="color: #008080;">get_class_methods</span>(<span style="color: #ff00ff;">__CLASS__</span><span style="color: #000000;">)); output_array(</span>'get_class_vars',<span style="color: #008080;">get_class_vars</span>(<span style="color: #ff00ff;">__CLASS__</span><span style="color: #000000;">)); output_array(</span>'get_object_vars',<span style="color: #008080;">get_object_vars</span>(<span style="color: #800080;">$this</span><span style="color: #000000;">)); } } </span><span style="color: #800080;">$testObj</span> = <span style="color: #0000ff;">new</span><span style="color: #000000;"> TestClass(); </span><span style="color: #0000ff;">print</span> "The following is output within TestClass.\n"<span style="color: #000000;">; </span><span style="color: #800080;">$testObj</span>-><span style="color: #000000;">publicFunction(); </span><span style="color: #0000ff;">print</span> "\nThe following is output out of TestClass.\n"<span style="color: #000000;">; output_array(</span>'get_class_methods',<span style="color: #008080;">get_class_methods</span>('TestClass'<span style="color: #000000;">)); output_array(</span>'get_class_vars',<span style="color: #008080;">get_class_vars</span>('TestClass'<span style="color: #000000;">)); output_array(</span>'get_object_vars',<span style="color: #008080;">get_object_vars</span>(<span style="color: #800080;">$testObj</span>));
运行结果如下:
<span style="color: #000000;">bogon:TestPhp liulei$ php class_exist_test.php The following is output within TestClass. get_class_methods..................... $key </span>= <span style="color: #800080;">0</span> => $value =<span style="color: #000000;"> privateFunction $key </span>= <span style="color: #800080;">1</span> => $value =<span style="color: #000000;"> publicFunction get_class_vars..................... $key </span>= publicVar => $value = <span style="color: #800080;">1</span><span style="color: #000000;"> $key </span>= privateVar => $value = <span style="color: #800080;">2</span><span style="color: #000000;"> $key </span>= staticPrivateVar => $value =<span style="color: #000000;"> hello $key </span>= staticPublicVar => $value =<span style="color: #000000;"> get_object_vars..................... $key </span>= publicVar => $value = <span style="color: #800080;">1</span><span style="color: #000000;"> $key </span>= privateVar => $value = <span style="color: #800080;">2</span><span style="color: #000000;"> The following is output out of TestClass. get_class_methods..................... $key </span>= <span style="color: #800080;">0</span> => $value =<span style="color: #000000;"> publicFunction get_class_vars..................... $key </span>= publicVar => $value = <span style="color: #800080;">1</span><span style="color: #000000;"> $key </span>= staticPublicVar => $value =<span style="color: #000000;"> get_object_vars..................... $key </span>= publicVar => $value = <span style="color: #800080;">1</span>
4. get_called_class和get_class:
string get_class ([ object $object = NULL ]) 获取参数对象的类名称。
string get_called_class (void) 静态方法调用时当前的类名称。
<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Base { </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test() { </span><span style="color: #008080;">var_dump</span><span style="color: #000000;">(get_called_class()); } } </span><span style="color: #0000ff;">class</span> Derive <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Base { } Base</span>::<span style="color: #000000;">test(); Derive</span>::<span style="color: #000000;">test(); </span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">get_class</span>(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Base())); </span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">get_class</span>(<span style="color: #0000ff;">new</span> Derive()));
运行结果如下:
<span style="color: #000000;">bogon:TestPhp$ php another_test_class.php </span><span style="color: #0000ff;">string</span>(<span style="color: #800080;">4</span>) <span style="color: #800000;">"</span><span style="color: #800000;">Base</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">string</span>(<span style="color: #800080;">6</span>) <span style="color: #800000;">"</span><span style="color: #800000;">Derive</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">string</span>(<span style="color: #800080;">4</span>) <span style="color: #800000;">"</span><span style="color: #800000;">Base</span><span style="color: #800000;">"</span> <span style="color: #0000ff;">string</span>(<span style="color: #800080;">6</span>) <span style="color: #800000;">"</span><span style="color: #800000;">Derive</span><span style="color: #800000;">"</span>
5. get_parent_class、is_a和is_subclass_of:
这三个函数都是和类的继承相关,所以我把他们归到了一起。
string get_parent_class ([ mixed $object ]) 获取参数对象的父类,如果没有父类则返回false。
bool is_a (object $object, string $class_name) 判断第一个参数对象是否是$class_name类本身或是其父类的对象。
bool is_subclass_of (mixed $object, string $class_name) 判断第一个参数对象是否是$class_name的子类。
<span style="color: #000000;">php </span><span style="color: #0000ff;">class</span><span style="color: #000000;"> Base { </span><span style="color: #0000ff;">static</span> <span style="color: #0000ff;">public</span> <span style="color: #0000ff;">function</span><span style="color: #000000;"> test() { </span><span style="color: #008080;">var_dump</span><span style="color: #000000;">(get_called_class()); } } </span><span style="color: #0000ff;">class</span> Derive <span style="color: #0000ff;">extends</span><span style="color: #000000;"> Base { } </span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">get_parent_class</span>(<span style="color: #0000ff;">new</span><span style="color: #000000;"> Derive())); </span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_a</span>(<span style="color: #0000ff;">new</span> Derive(),'Derive'<span style="color: #000000;">)); </span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_a</span>(<span style="color: #0000ff;">new</span> Derive(),'Base'<span style="color: #000000;">)); </span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_a</span>(<span style="color: #0000ff;">new</span> Base(),'Derive'<span style="color: #000000;">)); </span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_subclass_of</span>(<span style="color: #0000ff;">new</span> Derive(),'Derive'<span style="color: #000000;">)); </span><span style="color: #008080;">var_dump</span>(<span style="color: #008080;">is_subclass_of</span>(<span style="color: #0000ff;">new</span> Derive(),'Base'));
运行结果如下:
<span style="color: #000000;">bogon:TestPhp$ php another_test_class.php </span><span style="color: #000000;">string(4) "Base" bool(true) bool(true) bool(false) bool(false) bool(true)</span>

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



According to news from this site on June 24, at the keynote speech of the HDC2024 Huawei Developer Conference on June 21, Gong Ti, President of Huawei Terminal BG Software Department, officially announced Huawei’s self-developed Cangjie programming language. This language has been developed for 5 years and is now available for developer preview. Huawei's official developer website has now launched the official introductory tutorial video of Cangjie programming language to facilitate developers to get started and understand it. This tutorial will take users to experience Cangjie, learn Cangjie, and apply Cangjie, including using Cangjie language to estimate pi, calculate the stem and branch rules for each month of 2024, see N ways of expressing binary trees in Cangjie language, and use enumeration types to implement Algebraic calculations, signal system simulation using interfaces and extensions, and new syntax using Cangjie macros, etc. This site has tutorial access address: ht

This site reported on June 21 that at the HDC2024 Huawei Developer Conference this afternoon, Gong Ti, President of Huawei Terminal BG Software Department, officially announced Huawei’s self-developed Cangjie programming language and released a developer preview version of HarmonyOSNEXT Cangjie language. This is the first time Huawei has publicly released the Cangjie programming language. Gong Ti said: "In 2019, the Cangjie programming language project was born at Huawei. After 5 years of R&D accumulation and heavy R&D investment, it finally meets global developers today. Cangjie programming language integrates modern language features, comprehensive compilation optimization and Runtime implementation and out-of-the-box IDE tool chain support create a friendly development experience and excellent program performance for developers. "According to reports, Cangjie programming language is an all-scenario intelligence tool.

According to news from this site on June 21, Huawei’s self-developed Cangjie programming language was officially unveiled today, and the official announced the launch of HarmonyOSNEXT Cangjie language developer preview version Beta recruitment. This upgrade is an early adopter upgrade to the developer preview version, which provides Cangjie language SDK, developer guides and related DevEcoStudio plug-ins for developers to use Cangjie language to develop, debug and run HarmonyOSNext applications. Registration period: June 21, 2024 - October 21, 2024 Application requirements: This HarmonyOSNEXT Cangjie Language Developer Preview Beta recruitment event is only open to the following developers: 1) Real names have been completed in the Huawei Developer Alliance Certification; 2) Complete H

According to news from this site on June 22, Huawei yesterday introduced Huawei’s self-developed programming language-Cangjie to developers around the world. This is the first public appearance of Cangjie programming language. According to inquiries on this site, Tianjin University and Beijing University of Aeronautics and Astronautics were deeply involved in the research and development of Huawei’s “Cangjie”. Tianjin University: Cangjie Programming Language Compiler The software engineering team of the Department of Intelligence and Computing of Tianjin University joined hands with the Huawei Cangjie team to deeply participate in the quality assurance research of the Cangjie programming language compiler. According to reports, the Cangjie compiler is the basic software that is symbiotic with the Cangjie programming language. In the preparatory stage of the Cangjie programming language, a high-quality compiler that matches it became one of the core goals. As the Cangjie programming language evolves, the Cangjie compiler is constantly being upgraded and improved. In the past five years, Tianjin University

According to news from this site on June 21, before the HDC2024 Huawei Developer Conference, Huawei’s self-developed Cangjie programming language was officially unveiled, and the Cangjie official website is now online. The official website introduction shows that Cangjie programming language is a new generation programming language for all-scenario intelligence, focusing on "native intelligence, natural all-scenarios, high performance, and strong security." Integrate into the Hongmeng ecosystem to provide developers with a good programming experience. The official website attached to this site introduces as follows: Native intelligent programming framework embedded with AgentDSL, organic integration of natural language & programming language; multi-Agent collaboration, simplified symbolic expression, free combination of patterns, supporting the development of various intelligent applications. Innately lightweight and scalable runtime for all scenes, modular layered design, no matter how small the memory is, it can be accommodated; all-scenario domain expansion

According to news on June 21, this afternoon, Huawei Developer Conference 2024 will be officially opened. "Pure-blood Hongmeng" Harmony OS NEXT is naturally a top priority. According to the plan previously revealed by Yu Chengdong, the public beta may be officially announced this afternoon, and ordinary consumers can also try out "pure-blood Harmony". According to reports, the first batch of supported mobile phones are the Mate60 series and Pura70 series. It is worth noting that as a "pure-blooded Hongmeng", HarmonyOSNEXT has removed the traditional Linux kernel and AOSP Android open source code and developed the entire stack in-house. According to the latest report from Sina Technology, Huawei will also complete the last link of Hongmeng Ecosystem and expand its presence in the world.

Since the launch of ChatGLM-6B on March 14, 2023, the GLM series models have received widespread attention and recognition. Especially after ChatGLM3-6B was open sourced, developers are full of expectations for the fourth-generation model launched by Zhipu AI. This expectation has finally been fully satisfied with the release of GLM-4-9B. The birth of GLM-4-9B In order to give small models (10B and below) more powerful capabilities, the GLM technical team launched this new fourth-generation GLM series open source model: GLM-4-9B after nearly half a year of exploration. This model greatly compresses the model size while ensuring accuracy, and has faster inference speed and higher efficiency. The GLM technical team’s exploration has not

Produced by 51CTO technology stack (WeChat ID: blog51cto) Mistral released its first code model Codestral-22B! What’s crazy about this model is not only that it’s trained on over 80 programming languages, including Swift, etc. that many code models ignore. Their speeds are not exactly the same. It is required to write a "publish/subscribe" system using Go language. The GPT-4o here is being output, and Codestral is handing in the paper so fast that it’s hard to see! Since the model has just been launched, it has not yet been publicly tested. But according to the person in charge of Mistral, Codestral is currently the best-performing open source code model. Friends who are interested in the picture can move to: - Hug the face: https
