面向 例题


class Ren
{
    private $name;
    private $sex;
    private $age; //年龄必须在18-50之间
    
    function __construct($v)
    {
        $this->sex = $v;
    }
    //魔术方法set
    function __set($n,$v)
    {
        if($n=="age")
        {
            if($v>=18 && $v<=50)
            {
                $this->$n = $v;
            }
        }
        else 
        {
            $this->$n = $v;
        }
    }
    
    //魔术方法get
    function __get($n)
    {
        return $this->$n;
    }
    
    /*//设置age的值
    function setage($a)
    {
        if($a>=18 && $a<=50)
        {
            $this->age = $a;
        }
    }
    //获取age的值
    function getage()
    {
        return $this->age;
    }*/
    
    function say()
    {    
        echo "hello"; 
    }
    
    //析构方法
    function __destruct()
    {
        echo "这是一个析构方法";
    }
    
    function __tostring()
    {
        return "这个类是人类";
    }
    
    
}

$r = new Ren("");

//$r->setage(20);
//echo $r->getage();

$r->say();
//$r->age = 30;

//echo $r->age;

//$r->__get("age");

//$r->__set("age",20);

var_dump($r);


计算器

111111111111111


$a = 10;
$b = 5;


$jia = $a+$b;
$jian = $a-$b;
$cheng = $a*$b;
$chu = $a/$b





22222222222222222222222
class jisuan { public $a; public $b; function __construct($n,$m) { $this->a = $n; $this->b = $m; } 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; } function quyu() { return $this->a%$this->b; } } $j = new jisuan(10,5); echo $j->quyu();
原文地址:https://www.cnblogs.com/benpaodegegen/p/5993668.html