php面向对象

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>

<?php

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.执行时间特殊:造对象的时候就执行

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













?>



</body>
</html>
原文地址:https://www.cnblogs.com/chaochao00o/p/6186926.html