码迷,mamicode.com
首页 > Web开发 > 详细

PHP类(初学)

时间:2015-02-05 23:31:47      阅读:326      评论:0      收藏:0      [点我收藏+]

标签:php类 示例

类示例:Cat.php

<?php

class  Cat 
 {
	private  $name ;
	private $age;
	private $color;
	
	
	function __construct() 
	{
		$numargs  =  func_num_args ();
		if($numargs==3)
		{
			$this->name=func_get_arg(0);
			$this->age=func_get_arg(1);
			$this->color=func_get_arg(2);
		}
		
	}
/* 不支持	
 * function __construct($name,$age,$color)
	{
		$this->name=$name;
		$this->age=$age;
		$this->color=$color;
	} */
	
	
	public function  getName()
	{
		return $this->name;

	}
	
	public function setName($name)
	{
		return $this->name=$name;
	}
	
	
	public function getAge()
	{
		return $this->age;
	}
	public function setAge($age)
	{
		$this->age=$age;
	}
	
	public function getColor()
	{
		return $this->color;
	}
	public function setColor($color)
	{
		$this->color=$color;
	}
	
}





?>

对象:

<?php
header('Content-Type:text/html;charset=UTF-8');
require 'Cat.php';
$cat1 = new Cat('蔡倩倩',24,'白的');
/* 
 * $cat1 = new Cat();
$cat1->setName('蔡倩倩');
$cat1->setAge(24);
$cat1->setColor('白的');
 */

echo 'Cat1的姓名为:'.$cat1->getName().'<br/>年龄为:'.$cat1->getAge();

?>


PHP类(初学)

标签:php类 示例

原文地址:http://blog.csdn.net/w605283073/article/details/43538309

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!