Table of Contents
Definition:
Attribute overloading
Example
Method overloading
Home Backend Development PHP Tutorial PHP object-oriented overloading

PHP object-oriented overloading

Jun 06, 2018 am 10:05 AM
php object-oriented Overload

This article mainly introduces the object-oriented overloading of PHP, which has certain reference value. Now I share it with you. Friends in need can refer to it.

Definition:

1 ) dynamically "creates" the properties and methods of a class.

2) Achieved through magic method.

3) When calling a property or method of a class that is undefined or invisible in the current environment, the overloaded method will be called.

Attribute overloading

__set      赋值

__get      读取

__isset    判断是否存在

__unset    销毁
Copy after login

Example

// 属性的重载
class Person
{
    public $name = '小芳';
    protected $age = 18;

    public function __get($n)
    {
        //echo '试图读取不可访问的属性'.$n;

        if( $n == 'age'){
            return $this -> age;
        }else{
            return '你要查户口吗?';
        }
    }

    public function __set($n,$v)
    {
        //echo &#39;试图设置不可访问的属性&#39;,&#39;<br/>&#39;;
        $this -> $n = $v;
    }

    public function __isset($n)
    {
        echo &#39;判断不可访问的属性&#39;.$n.&#39;是否存在&#39;,&#39;<br/>&#39;;
    }

    public function __unset($n)
    {
        echo &#39;销毁不可访问的属性&#39;.$n,&#39;<br/>&#39;;
    }
}

$p1 = new Person();

// 读取
//echo $p1 -> age,&#39;<br/>&#39;;
//echo $p1 -> xxx,&#39;<br/>&#39;;

// 设置
//$p1 -> age = 30;
//echo $p1 -> age,&#39;<br/>&#39;;

// 判断存在与否
isset($p1 -> age);

// 销毁
unset($p1 -> age);
Copy after login

Method overloading

__call         调用不可访问的普通方法

__callStatic   调用不可访问的静态方法
Copy after login

Special note that when __callStatic is defined, it must be defined as a static method.

Example

<?php

class MyClass
{
    protected function func($n)
    {
        echo &#39;这是一个不可访问的方法&#39;;
        echo &#39;参数有&#39;.$n;
    }

    protected static function fun2()
    {
        echo &#39;受保护的静态方法&#39;;
    }

    public function __call($function_name,$args)
    {
        echo &#39;触发了不可访问的方法&#39;;
        var_dump($function_name);
        var_dump($args);
    }

    public static function __callStatic($function_name,$args)
    {
        echo &#39;触发了不可访问jing tai方法,静态!!!!&#39;;
        var_dump($function_name);
        var_dump($args);
    }
} 

// 实例化
$c1 = new MyClass();

$c1 -> func([1,2,3]);

$c1 -> func2([1,2,3]);
Copy after login

Related recommendations:

php object-oriented encapsulation

##php Object-oriented magic methods

php object-oriented static methods, properties and constants

The above is the detailed content of PHP object-oriented overloading. For more information, please follow other related articles on the PHP Chinese website!

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)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
1 months ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
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)

An in-depth explanation of PHP's object-oriented encapsulation An in-depth explanation of PHP's object-oriented encapsulation Aug 11, 2023 am 11:00 AM

In-depth interpretation of PHP object-oriented encapsulation Encapsulation is one of the three major characteristics of object-oriented programming. It refers to encapsulating data and operations on data in a class, hiding specific implementation details from external programs, and providing external interfaces. In PHP, the concept of encapsulation is implemented by using access modifiers (public, protected, private) to control the accessibility of properties and methods. First, let’s take a look at the role of access modifiers: public (public): Public properties and methods can

How to overload golang functions? How to overload golang functions? Apr 28, 2024 am 09:27 AM

Traditional function overloading is not supported in Go, but it can be simulated by the following techniques: Multiple return values: Functions with the same method signature but different return types can be overloaded. Variadics: Use the ... syntax to create functions that receive a variable number of arguments, allowing method calls to be handled with different signatures.

What is the difference between golang function overloading and polymorphism? What is the difference between golang function overloading and polymorphism? Apr 30, 2024 am 09:30 AM

Function overloading is not supported in Go language because it adopts duck typing and determines the value type based on the actual type. Polymorphism is achieved through interface types and method calls, and objects of different categories can respond in the same way. Specifically, by defining interfaces and implementing these methods, Go language can make objects of different types have similar behaviors, thereby supporting polymorphism.

How to implement polymorphism and overloading in Go language? How to implement polymorphism and overloading in Go language? Jun 10, 2023 am 10:25 AM

As a statically typed language, Go language does not seem to be able to implement polymorphism and overloading like dynamic languages. However, the Go language uses the characteristics of interfaces to achieve polymorphism, and the implementation of overloading is simpler and more accurate. Methods to implement polymorphism The interface in the Go language can implement polymorphism during the calling process. The interface can describe the behavior of an object. Any type that implements all methods of the interface can be called an instance of the interface type. In this way, polymorphism can be achieved by simply defining the interface type and implementing different concrete types. Below is a

Does PHP function support function overloading and function overriding? Does PHP function support function overloading and function overriding? Apr 19, 2024 am 10:06 AM

The PHP language does not support function overloading and function coverage because function overloading may cause ambiguity. Alternative: Use namespaces to isolate functions. Set parameter default values. Use variadic function arguments.

How to implement object version control and management through PHP object-oriented simple factory pattern How to implement object version control and management through PHP object-oriented simple factory pattern Sep 06, 2023 pm 02:39 PM

How to implement object version control and management through the PHP object-oriented simple factory model. When developing large and complex PHP projects, version control and management are very important. Through appropriate design patterns, we can better manage and control the creation and use of objects, thereby improving the maintainability and scalability of the code. This article will introduce how to use the PHP object-oriented simple factory pattern to implement object version control and management. The simple factory pattern is a design pattern for creating classes that instantiates specified objects through a factory class

What are the differences between nosql and mysql What are the differences between nosql and mysql Jan 28, 2023 pm 04:51 PM

Differences: 1. MySQL is a relational database, while NoSQL is non-relational. 2. MySQL’s strict mode restrictions are not easy to expand, while NoSQL is easy to expand. 3. MySQL requires a detailed database model before creating a database, which is not required in NoSQL. 4. MySQL provides a large number of reporting tools, but nosql does not. 5. Compared with MySQL, NoSQL provides a more flexible design. 6. The standard language used in MySQL is SQL, while NoSQL lacks a standard query language.

Is there any way to overload in php? How to achieve? Is there any way to overload in php? How to achieve? Mar 28, 2023 pm 01:54 PM

PHP is a very popular server-side scripting language used for developing web applications. However, for some beginners, understanding some concepts of PHP may cause some difficulties. This article will explore the concept of method overloading in PHP.

See all articles