Home  >  Article  >  Backend Development  >  这种程序怎么实现

这种程序怎么实现

WBOY
WBOYOriginal
2016-06-13 10:57:27799browse

这种程序如何实现
我想在一个类里面建两个方法如

PHP code
<!--Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->class ren{public function listing(){//code}public function listing($dd){//code}}

这样能行吗?或者有没有更好的办法呢?

------解决方案--------------------
PHP只允许一个函数或方法名有一种定义。只能走个弯路
class ren
{
private function _listing()
{
//code
}
public function listing($dd = null)
{
is_null($dd) && $this->_listing();
//code
}

}
(new ren())->listing();
------解决方案--------------------
函数法的重载?
对不起,php 不支持!
php 只是做做动态网页而已,没必要弄的那么复杂
如果 php 搞得跟 C++、Java 一样,我想他也就没有市场了

不过 php 提供了
func_get_arg
func_num_args
func_get_args
三个函数。完全可以解决你的问题,不过需要你自己编程实现
------解决方案--------------------
php不支持重载
其实也有很多高级语言不支持重载的,c++只是用了一个奇怪的方法(编译器级)进行重载;一般认为重载会导致程序阅读产生混乱
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