search
HomeBackend DevelopmentPHP TutorialOverloading of php __get __set isset unset __call __callStatic python static method import static import difference gcc static link static library

overloading in php is different from traditional object-oriented rewriting, such as in java:

<code>class A{

  public void  methodName(参数<span>1</span>);
  public void  methodName(参数<span>1</span>,参数<span>2</span>);
  public void  methodName(参数<span>1</span>,参数<span>2</span>,参数<span>3</span>);
  <span>...</span>}</code>

Overloading in php is an "elegant" error handling mechanism when calling non-existent properties or methods on a class or object. I don’t understand why this is also called overloading in PHP. It has nothing to do with overloading in traditional object-oriented programming.
PHP overloading relies on the magic methods __get() __set() isset() unset() __call() __callStatic(). When we access non-existent properties or methods, the system will automatically call these magic methods.

Code:

<code><span><span><?php </span>
header(<span>"content-type:text/html;charset=utf-8"</span>);

<span><span>class</span><span>Sample</span>{</span><span>public</span><span>$p1</span> = <span>1</span>;<span>//类中声明的属性p1</span><span>//保存被重载的数据</span><span>private</span><span>$data</span> = <span>array</span>(); 

 <span>/*
   在访问对象不存在的属性时__get被调用
   $name:变量名
  */</span><span><span>function</span><span>__get</span><span>(<span>$name</span>)</span>
  {</span><span>"<br>__get:: "</span>.<span>$name</span>;
    <span>if</span>(array_key_exists(<span>$name</span>, <span>$this</span>->data)){
        <span>return</span><span>$this</span>->data[<span>$name</span>];
    }

    <span>$trace</span> = debug_backtrace();
    trigger_error(
        <span>'访问类中不存在的属性'</span>.<span>$name</span>.
        <span>' 文件:'</span>.<span>$trace</span>[<span>0</span>][<span>'file'</span>].
        <span>' 所在行'</span>.<span>$trace</span>[<span>0</span>][<span>'line'</span>]
        ,E_USER_WARNING);
    <span>return</span><span>null</span>;

  }

  <span>/*
   对象不存在的属性赋值时__set被调用
   $name:变量名
   $value:变量值
  */</span><span><span>function</span><span>__set</span><span>(<span>$name</span>,<span>$value</span>)</span>{</span><span>echo</span><span>"<br> __set:: $name = $value "</span>;
     <span>$this</span>->data[<span>$name</span>] = <span>$value</span>;

  }

  <span>/*
   对象不存在的属性使用isset()或empty() 时__isset被调用
   $name:变量名
   $value:变量值
  */</span><span><span>function</span><span>__isset</span><span>(<span>$name</span>)</span>{</span><span>echo</span><span>"<br>isset::  $name "</span>;
    <span>return</span><span>isset</span>(<span>$this</span>->data[<span>$name</span>]);
  }


  <span>/*
   对象不存在的属性使用unset()时被调用
   $name:变量名
   $value:变量值
  */</span><span><span>function</span><span>__unset</span><span>(<span>$name</span>)</span>{</span><span>echo</span><span>"<br>__unset::  $name"</span>;
    <span>unset</span>(<span>$this</span>->data[<span>$name</span>]);
  }

  <span>/*
    类的对象的不存在的实例方法调用的时候,会自动调用本方法,该方法必须是public
   */</span><span>public</span><span><span>function</span><span>__call</span><span>(<span>$name</span> , <span>$arguments</span>)</span>{</span><span>if</span>(<span>$name</span>===<span>'f1'</span>){
        <span>echo</span><span>"<br>"</span>.<span>$name</span>.<span>"方法被调用,"</span>.<span>"参数:"</span>;
        var_dump(<span>$arguments</span>);
    }<span>else</span><span>if</span>(<span>$name</span>===<span>'f2'</span>){
        <span>echo</span><span>"<br>"</span>.<span>$name</span>.<span>"方法被调用,"</span>.<span>"参数:"</span>;
        var_dump(<span>$arguments</span>);
    }<span>else</span>{

        trigger_error(<span>"非法调用!"</span>,E_USER_WARNING);
    }

  }

  <span>/*
     类的对象的不存在的静态方法调用的时候,会自动调用本方法
     5.3.0 新增 __callStatic()魔术方法。可见性未设置为 public 或未声明为 static 的时候会产生一个警告 
   */</span><span>public</span><span>static</span><span><span>function</span><span>__callStatic</span><span>(<span>$name</span> , <span>$arguments</span>)</span>{</span><span>echo</span><span>"<br>"</span>.<span>$name</span>.<span>"静态方法被调用,"</span>.<span>"参数:"</span>;
        var_dump(<span>$arguments</span>);

  }

 }


<span>$s</span> = <span>new</span> Sample();

<span>echo</span><span>"<br>访问类中存在的实例属性:s->p1:: "</span>.<span>$s</span>->p1;


<span>//属性重载只能在对象中进行。在静态方法中,这些魔术方法将不会被调用。所以这些方法都不能被 声明为 static。从 PHP 5.3.0 起, 将这些魔术方法定义为 static 会产生一个警告。</span><span>//Fatal error: Access to undeclared static property: Sample::$p3</span><span>//echo "<br>访问类中不存在的静态属性不会调用魔术方法__get:".Sample::$p3;//错误</span><span>//访问类中不存在的属性,类中的魔术方法__get会被调用</span><span>echo</span><span>$s</span>->p2;

<span>//给类中不存在的属性赋值,类中的魔术方法__set会被调用</span><span>$s</span>->p2 = <span>88</span>;

<span>echo</span><span>'<br>'</span>.<span>$s</span>->p2; <span>//输出 88</span><span>//类中的魔术方法__isset会被调用</span>
var_dump(<span>isset</span>(<span>$s</span>->p2));
<span>//类中的魔术方法__isset会被调用</span>
var_dump(<span>empty</span>(<span>$s</span>->p2));

<span>//类中的魔术方法__unset会被调用</span><span>unset</span>(<span>$s</span>->p2);


<span>echo</span><span>'<br>'</span>.<span>$s</span>->p2; <span>//p2被销毁,报错</span><span>$s</span>->f1();
<span>$s</span>->f1(<span>1</span>,<span>2</span>);
<span>$s</span>->f1(<span>1</span>,<span>2</span>,<span>"hello"</span>);

<span>$s</span>->f2(<span>true</span>,<span>"111"</span>);

<span>$s</span>->f3();

<span>//调用对象不存在的静态方法</span>
Sample::say(<span>'hello'</span>);
Sample::say(<span>'hello'</span>,<span>'php'</span>);

<span>?></span></span></span></code>

static的作用,static electricity,static c语言,static 函数,java static,python static method,import static import区别,gcc static 链接静态库

').addClass('pre-numbering').hide(); $(this).addClass('has-numbering').parent().append($numbering); for (i = 1; i ').text(i)); }; $numbering.fadeIn(1700); }); });

The above introduces the overloading of php __get __set isset unset __call __callStatic, including static content. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement
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
How do you create and use an interface in PHP?How do you create and use an interface in PHP?Apr 30, 2025 pm 03:40 PM

The article explains how to create, implement, and use interfaces in PHP, focusing on their benefits for code organization and maintainability.

What is the difference between crypt() and password_hash()?What is the difference between crypt() and password_hash()?Apr 30, 2025 pm 03:39 PM

The article discusses the differences between crypt() and password_hash() in PHP for password hashing, focusing on their implementation, security, and suitability for modern web applications.

How can you prevent Cross-Site Scripting (XSS) in PHP?How can you prevent Cross-Site Scripting (XSS) in PHP?Apr 30, 2025 pm 03:38 PM

Article discusses preventing Cross-Site Scripting (XSS) in PHP through input validation, output encoding, and using tools like OWASP ESAPI and HTML Purifier.

What is autoloading in PHP?What is autoloading in PHP?Apr 30, 2025 pm 03:37 PM

Autoloading in PHP automatically loads class files when needed, improving performance by reducing memory use and enhancing code organization. Best practices include using PSR-4 and organizing code effectively.

What are PHP streams?What are PHP streams?Apr 30, 2025 pm 03:36 PM

PHP streams unify handling of resources like files, network sockets, and compression formats via a consistent API, abstracting complexity and enhancing code flexibility and efficiency.

What is the maximum size of a file that can be uploaded using PHP ?What is the maximum size of a file that can be uploaded using PHP ?Apr 30, 2025 pm 03:35 PM

The article discusses managing file upload sizes in PHP, focusing on the default limit of 2MB and how to increase it by modifying php.ini settings.

What is Nullable types in PHP ?What is Nullable types in PHP ?Apr 30, 2025 pm 03:34 PM

The article discusses nullable types in PHP, introduced in PHP 7.1, allowing variables or parameters to be either a specified type or null. It highlights benefits like improved readability, type safety, and explicit intent, and explains how to declar

What is the difference between the unset() and unlink() functions ?What is the difference between the unset() and unlink() functions ?Apr 30, 2025 pm 03:33 PM

The article discusses the differences between unset() and unlink() functions in programming, focusing on their purposes and use cases. Unset() removes variables from memory, while unlink() deletes files from the filesystem. Both are crucial for effec

See all articles

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

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.