PHP基础知识构造函数

来源:互联网 发布:c语言开根号函数 编辑:程序博客网 时间:2024/05/16 04:47
PHP基础知识构造函数
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/16 0016
 * Time: 15:05
 */
header("Content-type:text/html;charset=utf-8");
class Site{
    public $url="this is public";//public,private,protected;
   private $url2 "this is private";
    protected $url3 "this is protected";
    private $mySite;
    static public $myName;
    //构造函数。
   public function __construct($site)
    {
        $this->mySite $site;
        echo "我是构造函数<br>";
    }
    //析构函数
   public function __destruct()
    {
        // TODO: Implement __destruct() method.
       echo "<Br>我是析构函数";
    }

    public function printMySite(){
        echo $this->mySite;
    }

    //无参数,无返回值
   public function getSite(){
        echo "这是getSite()方法<br>";
        echo "<br>".$this->url2;
        echo "<br>".$this->url3;
    }
    private function pri(){
        echo "this is private function <br>";
    }
    protected function pro(){
        echo "this is protect function<br>";
    }
    //无参数,有返回值
   public function getName(){
        echo "这个函数有返回值,但没有传参<br>";
        return "zhangsan";
    }
    //有参数,无返回值
   public function setMyComputer($color,array $myArr,$weight="15kg"){
        echo $color "<br>";
        echo $weight "<br>";
        var_dump($myArr);
    }
    //有参数,有返回值
   public function setMyphone($price){
        return "¥3300";
    }
    //static方法
   static function calc($a,$b){
        $c $a $b;
        echo $c;
    }

}
//1、构造函数是在对象实例化时自动调用的
//2、构造函数有几个参数,实例化对象时就要传递几个参数
//$mySite = new Site("www.baidu.com");
//echo "<Br>";
//$mySite->printMySite();
//$mySite->getSite();
//echo "<br>";
//echo $mySite->url;
////echo $mySite->url2; private申明,是不被外部调用的。
////echo $mySite->url3;
////$mySite->pro();
//echo "<Br>";
//$mySite->getName();
//echo "<br>";
//echo $mySite->getName() . "<br>";
//
////相当于:
//$name = $mySite->getName();
//echo $name;
//echo "<br>";
////调用有参数,无返回值
//$mySite->setMyComputer("red",array("xiaom","zhans"),"19kg");
////调用有参数,有返回值
//echo "<bR>";
//echo $mySite->setMyphone("¥2080");
//echo "<br>";
//Site::calc(5,9);
//echo "<br>";
//$mySite->calc(3,6);
//echo "<br>";
//Site::$myName = "zhansan";
//echo Site::$myName;
<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2016/7/16 0016
 * Time: 17:45
 */
include "Site.php";
class SonSite extends Site{
    public function __construct($site)
    {
        parent::__construct($site);
        echo "我是SunSite做的事情";
    }
    public function __destruct()
    {
        $this->url3;
        parent::__destruct(); // TODO: Change the autogenerated stub

   }
}
$mySonSite new SonSite("www");
$mySonSite->printMySite();
0 0
原创粉丝点击