代码风格养成-初识

    关于代码风格的认识,如果学过汇编的话都会知道,一个子程序只有一个入口和一个出口,高级语言无论怎么return都不过是跳转到出口的位置;而一个函数不仅有入参,理应也有返回值(虽然代码中可以不返回任何值),基于一段时间的开发经验,然后突然来了灵感想了一套不错的代码风格和一丢丢web开发框架的思想;

代码如下:

<?php

//代码风格养成篇-初识

//err的抽象定义[err并不指错误,而是一种返回值格式的抽象]
class Err
{
    protected $errCode; //返回的code
    protected $errMsg;  //返回的msg

    public function __construct($code, $msg)
    {
        $this->setCode($code);
        $this->setMsg($msg);
    }

    public function setCode($code)
    {
        $this->errCode = $code;
    }

    public function setMsg($msg)
    {
        $this->errMsg = $msg;
    }

    public function getCode()
    {
        return $this->errCode;
    }

    public function getMsg()
    {
        return $this->errMsg;
    }
}

class Model
{ }

//一种较为简单的实例化
class SdErr extends Err
{
    public function __construct($code = '', $msg = '')
    {
        parent::__construct($code, $msg);
    }

    public function setSdErr($code = 0, $msg = '')
    {
        parent::setCode($code);
        parent::setMsg($msg);
    }

    public function getSdErr()
    {
        return [
            'code' => parent::getCode(),
            'msg' => parent::getMsg()
        ];
    }
}

//一个简单model的实例化
class SdModel extends Model
{
    private $err;       //返回的错误
    private $data;      //返回的数据

    public function __construct()
    {
        $this->err = new SdErr();
    }

    private function _getRet()
    {
        return [
            'err' => $this->err->getSdErr(),
            'data' => $this->data
        ];
    }

    private function _setRet($code = 0, $msg = '', $data = [])
    {
        $this->err->setSdErr($code, $msg);
        $this->data = $data;
    }

    public function __set($key, $value)
    {
        switch (strtolower($key)) {
            case 'errcode':
            case 'code':
                $this->err->setCode($value);
                break;
            case 'errmsg':
            case 'msg':
                $this->err->setMsg($value);
                break;
            default:
                throw Exception(sprintf('$this->%s is undefined', $key));
                break;
        }
    }

    public function __get($key)
    {
        if (isset($this->key)) {
            return $this->key;
        } else {
            throw Exception(sprintf('$this->%s is undefined', $key));
        }
    }

    //自认为不错的代码风格
    public function funcSd($params)
    {
        $this->_setRet();       //返回值初始化
        do {
            //代码逻辑
            /**
             * 说明,使用do...while...的目的是为了可以让代码逻辑随时break跳出逻辑,避免代码中有过多return,增强代码可读性,当然了可以嵌套
             */
            if (!isset($params['hello'])) {
                $this->errCode = 1;
                $this->errMsg = 'where you go?';
                break;  //业务逻辑中断,跳转到函数结束位置
            }
            $this->data['str'] = $params['hello'];
            $this->data['int'] = 1;
            for($i = 2; $i < 10; $i++) {
                $this->data['int'] *= $i;
                if($i === 9) {    
                    break 2;
                }
            }
            break;  //跳过while逻辑判断,意义大概就i是这不是一个“真正"的do...while()...语句
        } while (false);
        return $this->_getRet();
    }
}

//demo
$model = new SdModel();
$ret = $model->funcSd([
    'hello' => 'hope for you!'
]);
echo json_encode($ret).PHP_EOL;
$ret = $model->funcSd([
    'where' => 'i am here!'
]);
echo json_encode($ret);
发布了31 篇原创文章 · 获赞 3 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_36557960/article/details/97835435