PHP學習(3)

小溪彼岸發表於2016-06-19

1、類

  • 類的建立:
<?php
class People
{
  //例項方法
//  public  function  __construct(){
//      echo 'Construct a man';
//  }

    public  function  __construct($age,$name,$sex){

        $this->_age  = $age;
        $this->_name = $name;
        $this->_sex  = $sex;

        People::$NUM ++;
        if(People::$NUM>People::MAX_MAN_NUM){
            throw new Exception('不能建立更多的人了');
        }
    }

    public function getAge(){
        return $this->_age;
    }

    public function getName(){
        return $this->_name;
    }

    public  function  getSex(){
        return  $this->_sex;
    }

    public  function  sayhi(){
        echo $this->_name.'say hi'.'<br>';
    }

    private $_age ,$_name,$_sex;

    private  static  $NUM = 0;

    const  MAX_MAN_NUM = 200;

    public  static  function sayHello(){
        echo 'Hello People';
    }
  • 類的呼叫:
$person = new People(12,'zw','男');
$sex    = $person->getSex();
echo  $sex;
echo "<br>";
  • 類的繼承:
<?php
require_once 'People.php';
class Man extends  People
{
    public  function  __construct($age,$name){
        parent::__construct($age,$name,'男');
    }

    public  function sayhi(){

        parent::sayhi();

        echo 'man say hi'.'<br>';
    }

}

呼叫:

require_once 'Man.php';
$man = new Man(12,'周巍');
$man->sayhi();

2、名稱空間

<?php
namespace zw;


class  Hello
{
    public  function  sayHello(){
        echo 'Hello Php zw';
    }
}
<?php
namespace zww;


class Hello
{
    public  function  sayHello(){
        echo 'Hello Php zww';
    }
}

呼叫:

require_once 'zw/Hello.php';
require_once 'zww/Hello.php';

$h = new \zw\Hello();
$h->sayHello();

echo '<br>';

$h2 = new \zww\Hello();
$h2->sayHello();

3、常用變數

$_POST變數
含義:代表使用者通過表單以post方式提交的時候所提交的所有資料–這個稱為post資料。

<form method="post"></form>

demo:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<form action="getPost.php" method="post">
    資料1:<input type="text" name="data1">
    <br/>
    資料2:<input type="text" name="data2">
    <br/>
   <input type="submit" value="提交">
</form>
</body>
</html>

處理類:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
</head>
<body>
<?php
if(!empty($_POST))
{
  $d1 = $_POST['data1'];
  $d2 = $_POST['data2'];
  echo "d1 = $d1,d2 = $d2";
  echo "<hr/>";
  echo($_POST);
}else{
    echo "非法的頁面訪問";
}
?>
</body>
</html>

效果圖:

這裡寫圖片描述