通过指定函数/方法形参类型提高PHP代码可靠性

简介: 指定形参类型是PHP 5就支持的一项特性。形参支持array - 数组、 object - 对象两种类型。class User{ public $name; public $password; function __construct($name,$password){ ...

指定形参类型是PHP 5就支持的一项特性。形参支持array - 数组、 object - 对象两种类型。

class User{
    public $name;
    public $password;
    function __construct($name,$password){
        $this->name=$name;
        $this->password=$password;
    }
}

//参数可以指定对象类型

function f1(User $user){
    echo $user->name,”,”,$user->password;
}

//参数可以指定数组类型

function f2(array $arr){}

//参数不可以指定基本类型,下面一句会出错

function f3(string $s){}

那对于我们最常见的需求,如强制参数类型是字符串或整型,怎么办呢?

在不考虑转换到Facebook的HHVM运行环境下的前提下,就用不了Hack语言。在没有Hack语言的情况下,就得自行定义一些基本类型类来完成相应的功能。

以下代码纯属思考,未经项目实证,对于相应性能或灵活性的影响需要在项目中实战评估。

class CString {
    private $_val = '';
    
    public function __construct($str = '') {
        if (!is_string($str)) {
            throw new Exception('Illegal data type');
        }
        $this->_val = $str;
    }
    
    public function __toString() {
        return $this->_val;
    }
}

class CInteger {
    private $_val = 0;
    
    public function __construct($str = 0) {
        if (!is_int($str)) {
            throw new Exception('Illegal data type');
        }
        $this->_val = $str;
    }
    
    public function __toString() {
        return (string) $this->_val;
    }
}

实际调用函数

function findUserByName(CString $name) {
     $sql = '....' . $name;
    //code
}
function findUserById(CInteger $id) {
     $sql = '.... AND uid=' . $id;
    //code
}

再往前走,对于集合型的数据呢? Yii框架中定义过一些相关的集合类,基本可以解决此类问题。

如CTypedList:

class CTypedList extends CList
{
    private $_type;

    /**
     * Constructor.
     * @param string $type class type
     */
    public function __construct($type)
    {
        $this->_type=$type;
    }

    /**
     * Inserts an item at the specified position.
     * This method overrides the parent implementation by
     * checking the item to be inserted is of certain type.
     * @param integer $index the specified position.
     * @param mixed $item new item
     * @throws CException If the index specified exceeds the bound,
     * the list is read-only or the element is not of the expected type.
     */
    public function insertAt($index,$item)
    {
        if($item instanceof $this->_type)
            parent::insertAt($index,$item);
        else
            throw new CException(Yii::t('yii','CTypedList<{type}> can only hold objects of {type} class.',
                array('{type}'=>$this->_type)));
    }
}

而对于单纯的数组,能怎么办呢?

目录
相关文章
|
22天前
|
缓存 PHP 开发者
PHP中的自动加载机制及其优化方法
传统的PHP开发中,经常会遇到类文件加载繁琐、效率低下的情况,而PHP的自动加载机制能够很好地解决这一问题。本文将深入探讨PHP中的自动加载机制,介绍其原理及实现方式,并提出了一些优化方法,帮助开发者提升代码加载效率,提高应用性能。
|
1月前
|
SQL 缓存 PHP
PHP技术探究:优化数据库查询效率的实用方法
本文将深入探讨PHP中优化数据库查询效率的实用方法,包括索引优化、SQL语句优化以及缓存机制的应用。通过合理的优化策略和技巧,可以显著提升系统性能,提高用户体验,是PHP开发者不容忽视的重要议题。
|
1月前
|
PHP 开发者
深入理解PHP7的返回类型声明
【2月更文挑战第29天】 在PHP 7中,一个新的特性被引入,即返回类型声明。这是一个强大的工具,它允许开发者明确指定函数或方法应返回的数据类型。本文将探讨这一特性的基本用法,以及如何利用它来提高代码的可读性和健壮性。
|
2天前
|
存储 SQL 缓存
记录如何用php做一个网站访问计数器的方法
创建简单网站访问计数器,可通过存储访问次数的文件或数据库。首先,创建`counter.txt`存储计数,然后在`counter.php`中编写PHP代码以读取、增加并显示计数,使用`flock`锁定文件避免并发问题。网页通过包含`counter.php`展示计数。对于高流量网站,推荐使用数据库确保原子性和并发处理能力,或利用缓存提升性能。注意,实际生产环境可能需更复杂技术防止作弊。
|
1月前
|
PHP 开发者 UED
PHP 中的异常处理:提高代码健壮性的关键
【2月更文挑战第28天】在 PHP 开发中,异常处理是确保应用程序稳定性和可靠性的重要环节。本文将深入探讨 PHP 异常的概念、类型及其处理机制,并通过实例演示如何有效地捕获和处理异常,以增强代码的健壮性和用户体验。
|
1月前
|
存储 数据处理 PHP
PHP变量类型
PHP变量类型
18 0
|
2月前
|
PHP
从建站到拿站 -- PHP判断循环及函数
从建站到拿站 -- PHP判断循环及函数
12 0
|
2月前
|
PHP
从PHP开始学渗透 -- 函数
从PHP开始学渗透 -- 函数
8 0
php案例:判断这个是繁体字还是简体字(满足绝大部分字符)用php函数
php案例:判断这个是繁体字还是简体字(满足绝大部分字符)用php函数
php案例:判断这个是繁体字还是简体字(满足绝大部分字符)用php函数
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)
php案例:判断这个文件是什么编程语言代码的文件(判断java或者php)