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

1211php面向对象

时间:2016-12-12 01:27:54      阅读:213      评论:0      收藏:0      [点我收藏+]

标签:关联数组   nbsp   索引   对象   var_dump   prot   ret   面向   function   

首先需要定义数组,$attr = array(直接给元素1,2,3)索引数组

关联数组 $attr = array("one"=>1,2,3)

for($i=0;$i<count($attr);$i++) for循环遍历索引数组  不能遍历关联数组

 

 

foreach遍历 关联索引都可以遍历

foreach($attr as $k=>$v)
{
$v;
}

面向对象 类 和 对象

例子:

$yuan = new Yuan();
$yuan->banjing = 10;
echo $yuan->MianJi();
var_dump($yuan);

//$this关键字在类里面代表该对象

//造一个大圆
$maxyuan = new Yuan();
$maxyuan->banjing = 10;

//造一个小圆
$minyuan = new Yuan();
$minyuan->banjing = 5;

echo $maxyuan->MianJi()-$minyuan->MianJi();

 

class YunSuan
{
public $a=10;
public $b=5;

//构造方法
function __construct($a1,$b1)
{
$this->a = $a1;
$this->b = $b1;
}

//析构方法,在对象内存释放的时候执行
function __destruct()
{
echo "该对象释放了";
}

private function Jia()
{
return $this->a+$this->b;
}

function Jian()
{
return $this->a-$this->b;
}

function Cheng()
{
return $this->a*$this->b;
}

function Chu()
{
return $this->a/$this->b;
}


}

//造对象
$y = new YunSuan(10,5);

var_dump($y);
echo $y->Chu();


//访问修饰符
//public 公有的,任何地方都可以访问
//protected 受保护的,只能在该类或该类的子类中访问
//private 私有的,只能在该类中访问

//__开头的方法在面向对象里面成为魔术方法

//构造函数
//1.写法特殊:方法名特殊
//2.执行时间特殊:造对象的时候就执行

//对对象里面的成员进行初始化

 

1211php面向对象

标签:关联数组   nbsp   索引   对象   var_dump   prot   ret   面向   function   

原文地址:http://www.cnblogs.com/xiaoming-6/p/6160914.html

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