Multiple functions use the same name, but the parameter list, that is, the number of parameters or (and) data type, can be different. When calling, although the method name is the same, the corresponding function can be automatically called according to the parameter list.
PHP4 only implements some simple object-oriented functions, but the support for objects will be much more powerful in PHP5.
For polymorphic implementation, PHP4 only supports override but not overload. But we can use some tricks to "simulate" the implementation of overloading.
Although PHP5 can support overwriting and overloading, the specific implementation of overloading is still quite different from other languages.
1. "Simulating" overloading in PHP4
Try the following code:
//Choose to execute different methods according to the number of parameters (simulating "overloading" in PHP4 (a type of polymorphism) )
class Myclass
{
function Myclass()
{
$method = "method" . func_num_args();
$this->$method();
}
function method1($x)
{
echo "method1";
}
function method2($x, $y)
{
echo 'method2';
}
}
//Using this class is transparent to users through additional processing in the class of:
$obj1 = new Myclass('A'); //method1 will be called
$obj2 = new Myclass('B','C'); //method2 will be called
?>
In the above code, By using the func_num_args() function in the constructor to get the number of parameters, the method1 or method2 method can be automatically executed. We can improve the above example by combining the functions func_get_arg(i) and func_get_args() and use it in PHP5. Overloading
First look at the following example:
Copy the code as follows:class Myclass
{
public $attriable;
public $one = "this is one";
public $two = "this is two";
function __construct()
}
function one($one)
$this->one=$one;
$this->attriable = $this->one;
}
function one($one, $two)
$this->one=$one;
$this->two=$two;
$this->attriable = $this-> one . $this->two;
}
function display()
echo $this->attriable;
}
}
$one = "this is my class";
$myclass = new myclass();
$myclass->one($one);
$myclass->display();
$myclass->one($one, $two) ;
//The approach in this example is incorrect in PHP!
In PHP5, overloading can be performed through several special methods __get, __set, and __call. PHP will call these methods when the Zend engine tries to access a member and cannot find it.
In the following example, __get and __set replace all accesses to the attribute variable array. If necessary, you can also implement any type of filtering you want. For example, a script can disable setting property values, start with a certain prefix, or include a certain type of value. The __call method illustrates how you can call an undefined method. When you call an undefined method, the method name and the parameters received by the method will be passed to the __call method, and PHP returns the value of __call to the undefined method.
The code is as follows:class Overloader
{
private $properties = array();
function __get($property_name)
if(isset($this->properties[$property_name] ))
{
return($this->properties[$property_name]);
}
else
{
return(NULL);
}
}
function __set($property_name, $value)
$ this->properties[$property_name] = $value;
}
public function __call($method, $p)
{
print("Invoking $method()
n");
//print("Arguments: ");
//print_r($args);
if($method == 'display')
{
if(is_object($p[0]))
$this->displayObject($p[0]);
else
if(is_array($p[ 0]))
$this->displayArray($p[0]);
else
$this->displayScalar($p[0]);
}
}
public function displayObject($p)
{
echo ("What you passed in is an object, the content is as follows:
");
print_r($p);
echo "
";
}
public function displayArray($p)
{
echo ("What you passed in is an array, the content is as follows:
");
print_r($p);
echo "
";
}
public function displayScalar($p)
{
echo ("What you passed in is a separate variable with the following content:
" . $p);
echo "
";
}
}
$o = new Overloader();
//Call __set() to assign a value to a non-existent attribute variable
$o->dynaProp = "Dynamic Content";
//Call __get()
print($o->dynaProp . "< ;br>n");
//Call __call()
//$o->dynaMethod("Leon", "Zeev");
$o->display(array(1,2, 3));
$o->display('Cat');
?>
In the above code, the display() method is called, and the corresponding code segment in the class can be called according to the type and number of parameters. , thus realizing the overloading of object methods.
Thank you for reading. If you want more related content, please pay attention to the PHP Chinese website (www.php.cn)!

如何使用Go语言实现面向对象的事件驱动编程引言:面向对象的编程范式被广泛应用于软件开发中,而事件驱动编程是一种常见的编程模式,它通过事件的触发和处理来实现程序的流程控制。本文将介绍如何使用Go语言实现面向对象的事件驱动编程,并提供代码示例。一、事件驱动编程的概念事件驱动编程是一种基于事件和消息的编程模式,它将程序的流程控制转移到事件的触发和处理上。在事件驱动

解析PHP面向对象编程中的享元模式在面向对象编程中,设计模式是一种常用的软件设计方法,它可以提高代码的可读性、可维护性和可扩展性。享元模式(Flyweightpattern)是设计模式中的一种,它通过共享对象来降低内存的开销。本文将探讨如何在PHP中使用享元模式来提高程序性能。什么是享元模式?享元模式是一种结构型设计模式,它的目的是在不同对象之间共享相同的

python是面向对象的。Python语言在设计之初,就定位为一门面向对象的编程语言,“Python中一切皆对象”就是对Pytho 这门编程语言的完美诠释。类和对象是Python的重要特征,相比其它面向对象语言,Python很容易就可以创建出一个类和对象;同时,Python也支持面向对象的三大特征:封装、继承和多态。

go语言既不是面向对象,也不是面向过程,因为Golang并没有明显的倾向,而是更倾向于让编程者去考虑该怎么去用它,也许它的特色就是灵活,编程者可以用它实现面向对象,但它本身不支持面向对象的语义。

如何使用Go语言实现面向对象的数据库访问引言:随着互联网的发展,大量的数据需要被存储和访问,数据库成为了现代应用开发中的重要组成部分。而作为一门现代化、高效性能的编程语言,Go语言很适合用来处理数据库操作。而本文将重点讨论如何使用Go语言实现面向对象的数据库访问。一、数据库访问的基本概念在开始讨论如何使用Go语言实现面向对象的数据库访问之前,我们先来了解一下

面向对象是软件开发方法,一种编程范式。是一种将面向对象的思想应用于软件开发过程并指导开发活动的系统方法。这是一种基于“对象”概念的方法论。对象是由数据和允许的操作组成的包,它与目标实体有直接的对应关系。对象类定义了一组具有类似属性的对象。面向对象是基于对象的概念,以对象为中心,以类和继承为构建机制,认识、理解和描绘客观世界,设计和构建相应的软件系统。

PHP作为一种广泛使用的编程语言,已成为构建动态网站和网络应用程序的首选语言之一。其中,面向对象编程(OOP)的概念和技术越来越受到开发者的欢迎和推崇。本篇文章将为读者提供PHP面向对象编程的入门指南,介绍OOP的基本概念,语法和应用。什么是面向对象编程(OOP)?面向对象编程(Object-OrientedProgramming,简称OOP),是一种编程

随着计算机技术的不断进步,编程语言也在不断发展与更新,每一种新的编程语言都有其自己独特的特点和优势。其中,Go语言作为一种比较新的编程语言,已经逐渐受到了开发者们的关注和喜爱。其中,面向对象编程方法是Go语言编程的重要组成部分,下面我们就来一起了解一下Go语言中的面向对象编程方法。一、Go语言中的面向对象编程面向对象编程(OOP,Object-Oriente


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

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse
Integrate Eclipse with SAP NetWeaver application server.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.