Home > Backend Development > PHP Tutorial > 子类重写父类属性的有关问题。求解惑

子类重写父类属性的有关问题。求解惑

WBOY
Release: 2016-06-13 12:33:33
Original
904 people have browsed it

子类重写父类属性的问题。。。。求解惑

<br />
<?php<br />
	/**<br />
	 * 	子类重写父类方法和属性<br />
	 **/<br />
	class Employer<br />
	{<br />
		protected $sal=3000;<br />
		public function getSal()<br />
		{<br />
			return $this->sal;<br />
		}<br />
		<br />
	}<br />
<br />
	class Manager extends Employer<br />
	{<br />
		protected $sal=5000;<br />
		public function getParentSal()<br />
		{<br />
			return parent::getSal(); //调用父类方法 返回父类属性,为什么会是5000<br />
		}<br />
		public function getSal()<br />
		{<br />
			return $this->sal;<br />
		}<br />
	}<br />
	$manager = new Manager;<br />
	echo $manager->getParentSal().PHP_EOL;//5000 这是为什么也是5000呢,但是在5.3以下版本好像是3000,求解释????<br />
	echo $manager->getSal();//5000<br />
?><br />
Copy after login

php 子类重写父类属性的问题
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