phpDOC

1.filter_var

 

<?php
if(!filter_has_var(INPUT_GET, "email"))
{
    echo("没有 email 参数");
}
else
{
    if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL))
    {
        echo "不是一个合法的 E-Mail";
    }
    else
    {
        echo "是一个合法的 E-Mail";
    }
}

  

$filters = array
(
    "name" => array
    (
        "filter"=>FILTER_SANITIZE_STRING
    ),
    "age" => array
    (
        "filter"=>FILTER_VALIDATE_INT,
        "options"=>array
        (
            "min_range"=>1,
            "max_range"=>120
        )
    ),
    "email"=> FILTER_VALIDATE_EMAIL
);
 
$result = filter_input_array(INPUT_GET, $filters);
 
if (!$result["age"])
{
    echo("年龄必须在 1 到 120 之间。<br>");
}
elseif(!$result["email"])
{
    echo("E-Mail 不合法<br>");
}
else
{
    echo("输入正确");
}
        if (file_exists("upload/" . $_FILES["file"]["name"]))
        {
            echo $_FILES["file"]["name"] . " 文件已经存在。 ";
        }
        else
        {
            // 如果 upload 目录不存在该文件则将文件上传到 upload 目录下
            move_uploaded_file($_FILES["file"]["tmp_name"], "upload/" . $_FILES["file"]["name"]);
            echo "文件存储在: " . "upload/" . $_FILES["file"]["name"];
        }

 

 
function convertSpace($string)
{
    return str_replace("_", ".", $string);
}
 
$string = "www_runoob_com!";
 
echo filter_var($string, FILTER_CALLBACK,
array("options"=>"convertSpace"));

   所谓多态性是指一段程序能够处理多种类型对象的能力,比如说在公司上班,每个月财务发放工资,同一个发工资的方法,在公司内不同的员工或是不同职位的员工,都是通过这个方法发放的,但是所发的工资都是不相同的。所以同一个发工资的方法就出现了多种形态。对于面向对象的程序来说,多态就是把子类对象赋值给父类引用,然后调用父类的方法,去执行子类覆盖父类的那个方法,但在PHP里是弱类型的,对象引用都是一样的不分父类引用,还是子类引用。

<?php
// 定义了一个形状的接口,里面有两个抽象方法让子类去实现
interface Shape {
    function area();
    function perimeter();
}
 
// 定义了一个矩形子类实现了形状接口中的周长和面积
class Rect implements Shape {
    private $width;
    private $height;
 
    function __construct($width, $height) {
        $this->width = $width;
        $this->height = $height;
    }
 
    function area() {
        return "矩形的面积是:" . ($this->width * $this->height);
    }
 
    function perimeter() {
        return "矩形的周长是:" . (2 * ($this->width + $this->height));
    }
}
 
// 定义了一个圆形子类实现了形状接口中的周长和面积
class  Circular implements Shape {
    private $radius;
 
    function __construct($radius) {
        $this->radius=$radius;
    }
 
    function area() {
        return "圆形的面积是:" . (3.14 * $this->radius * $this->radius);
    }
 
    function perimeter() {
        return "圆形的周长是:" . (2 * 3.14 * $this->radius);
    }
}
 
// 把子类矩形对象赋给形状的一个引用
$shape = new Rect(5, 10);
echo $shape->area() . "<br>";
echo $shape->perimeter() . "<br>";
 
// 把子类圆形对象赋给形状的一个引用
$shape = new Circular(10);
echo $shape->area() . "<br>";
echo $shape->perimeter() . "<br>";
?>

  因为接口是一种特殊的抽象类,里面所有的方法都是抽象方法,所以接口也不能产生实例对象; 它也做为一种规范,所有抽象方法需要子类去实现。

魔术方法

http://www.cnblogs.com/phpgo/p/5665463.html

 

posted on 2017-07-23 19:43  coding-farmer  阅读(176)  评论(0编辑  收藏  举报

导航