Home > Backend Development > PHP Tutorial > php指定参数部类

php指定参数部类

WBOY
Release: 2016-06-13 12:56:34
Original
1210 people have browsed it

php指定参数类型

	public function execute(PwThreadDataSource $ds) {<br />
		$this->_ds = $ds;
Copy after login


都说php声明变量的时候不要指明类型,那这个参数为什么指明了为 PwThreadDataSource类型?而且这是个抽象类,抽象类不是不能声明对象的吗?没搞懂,难道多态?
abstract class PwThreadDataSource {<br />
<br />
	protected $urlArgs = array();<br />
<br />
	/**<br />
	 * 获取帖子统计数<br />
	 *<br />
	 * @return int<br />
	 */<br />
	abstract public function getTotal();<br />
	<br />
	/**<br />
	 * 获取帖子<br />
	 *<br />
	 * @param int $limit 获取条目<br />
	 * @param int $offset 帖子起始偏移量<br />
	 * @return array<br />
	 */<br />
	abstract public function getData($limit, $offset);
Copy after login


------解决方案--------------------
execute(PwThreadDataSource $ds)
这个 $ds 是形参,这个声明只是为了约定调用者能传入PwThreadDataSource类或其子类的对象,以供函数内代码调用。
这样做也是为了协调编码,至少我不用看注释或代码,就知道该传个怎样的参数进去。
Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template