书写技术成长之路

Git 生成SSH key

Git 生成SSH key的步骤

  1. Generating a new SSH key ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
  2. Ensure ssh-agent is enabled eval "$(ssh-agent -s)"
  3. Add your SSH key to the ssh-agent ssh-add ~/.ssh/id_rsa

检查SSH是否成功

ssh -T -p 443 git@ssh.github.com

介绍PHP Trait

PHP Trait的特性

Trait是为类似PHP的单继承语言而准备的一种代码附中机制。 Trait为了减少减少单继承语言的限制,使开发人员能够自由地在不同层次结构内独立的类中复用method。 Trait和Class相似,但仅仅旨在用细粒度和一致的方式来组合功能。无法通过trait自身来实例化。它为传统继承增加了水平特性的组合;也就是说,应用的几个class 之间不需要继承。

PHP Trait的几个特性

  • Trait可以定义属性
  • Trait可以使用多个Trait
  • Trait可以修改方法的访问控制
  • 类可以通过逗号分隔,在use声明列出多个trait
  • 从基类继承的成员会被trait插入的成员所覆盖。优先顺序是来自当前类的成员覆盖了trait的方法,而trait则覆盖了被继承的方法。
<?php

trait HelloTrait
{
    public function sayHello()
    {
        echo "hello ";
    }
}

trait WorldTrait
{
    public function sayWorld()
    {
        echo "World"."\n";
    }
}

class HelloWorld
{
    use HelloTrait, WorldTrait;
}

$a = new HelloWorld;
$a->sayHello();
$a->sayWorld();

参考地址

  1. php-traits culttt
  2. php-official-documents

在RedHat 7上搭建PHP开发环境

基础

  1. 为确保所有软件都处于最新状态,首先需要执行sudo yum update -y来保证拥有最新的安全更新和缺陷修复。 (-y 选项安装更新时不提示确认)
  2. 安装开发工具 sudo yum -y group install "Development Tools"
  3. 安装wget sudo yum install -y wget
  4. 更改时区 rm -rf /etc/localtime ln -s /usr/share/zoneinfo/Asia/Shanghai /etc/localtime
  5. 安装VIM sudo yum install -y vim
  6. 获取最新包 rpm -Uvh https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm
  7. rpm -Uvh https://mirror.webtatic.com/yum/el7/webtatic-release.rpm

安装PHP 7

  1. 运行yum install php70w php70w-fpm php70w-common php70w-cli php70w-mysql php70w-gd php70w-mbstring
  2. 运行php -v 查看php是否安装成功
  3. 把php-fpm加入开机启动 sudo chkconfig php-fpm on

安装Nginx 1.8

  1. 安装Nginx yum install -y nginx18
  2. 把nginx加入开机启动 sudo chkconfig nginx on
  3. 启动nginx sudo nginx

安装MySQL 5.6

  1. 下载rpm安装包 wget http://repo.mysql.com/mysql-community-release-el7-5.noarch.rpm
  2. 安装rpm包 rpm -ivh mysql-community-release-el7-5.noarch.rpm
  3. 检查包是否安装成功 ls -al /etc/yum.repos.d/mysql-community*
  4. 安装Mysql 5.6 sudo yum install -y mysql-server
  5. 将MySQL加入开机启动 chkconfig mysqld on
  6. 启动MySQL服务 sudo service mysqld start
  7. 设置MySQL密码 mysql_secure_installation 或者登录mysql后通过如下命令设置 use mysqlupdate user set password=PASSWORD("GIVE-NEW-ROOT-PASSWORD") where User='root';flush privileges;

配置vim

编辑 /etc/vimrc

" 设置缩进
set tabstop=4
set softtabstop=4
set expandtab
set number " 显示行号
set cursorline " highlight current line
set incsearch " search as characters are extered
set hlsearch " highlight matches

配置Nginx

upstream phpfcgi {
    server 127.0.0.1:9000;
}

server {
    listen 80;
    server_name example.com;

    error_log /srv/logs/dev_error.log;
    access_log /srv/logs/dev_acces.log;

    root /srv/www/dev;
    index index.php index.html index.htm;

    location / {
        try_files $uri $uri/ /index.php$is_args$args;
    }

    # pass the PHP scripts to FastCGI server listening
    location ~ \.php$ {
        try_files $uri /index.php =404;
        fastcgi_pass phpfcgi;
        fastcgi_index index.php;
        fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name;
        include fastcgi_params;
    }
}

参考地址

1. install nginx

2. install php

3. install mysql

初探 PHP Closure

当看Laravel源码的时候,看到这段代码,竟然没看出来$c$parameters是怎么传过来的,

声明方法

    /**
     * Get the Closure to be used when building a type.
     *
     * @param  string  $abstract
     * @param  string  $concrete
     * @return \Closure
     */
    protected function getClosure($abstract, $concrete)
    {
        return function ($c, $parameters = []) use ($abstract, $concrete) {
            $method = ($abstract == $concrete) ? 'build' : 'make';

            return $c->$method($concrete, $parameters);
        };
    }

首先看这个方法,没有定义变量$c$parameters,怎么会不报错呢。直到搜索后才看到这是Closure的用法,真实长见识了。

Closure是用于代表匿名函数的类,所以它的用法和匿名函数(Anonymous functions)是一样的。 那么Closure是怎么使用的呢?

Closure通过use引用外部变量到当前环境中,不在use后面引用的参数是在Closure被调用的时候传入的。 通过以下例子来说明

<?php

class ClosureTest
{
    public function getClosure($name)
    {
        // use将外部变量引入进来
        return function($score) use ($name) {
            return $name."------>".$score."\n";
        };
    }
}

$my_closure = new ClosureTest;
$closure = $my_closure->getClosure("Jackson");  // 变量$name在实例化一个Closure的时候传进来
echo $closure(80);  // 变量$score在Closure被调用的时候传进来

这是laravel调用Closure的方法

/**
 * Instantiate a concrete instance of the given type.
 *
 * @param  string  $concrete
 * @param  array   $parameters
 * @return mixed
 *
 * @throws \Illuminate\Contracts\Container\BindingResolutionException
 */
public function build($concrete, array $parameters = [])
{
    // If the concrete type is actually a Closure, we will just execute it and
    // hand back the results of the functions, which allows functions to be
    // used as resolvers for more fine-tuned resolution of these objects.
    // Laravel调用closure的方式
    if ($concrete instanceof Closure) {
        return $concrete($this, $parameters);
    }

    $reflector = new ReflectionClass($concrete);

    // If the type is not instantiable, the developer is attempting to resolve
    // an abstract type such as an Interface of Abstract Class and there is
    // no binding registered for the abstractions so we need to bail out.
    if (! $reflector->isInstantiable()) {
        $message = "Target [$concrete] is not instantiable.";

        throw new BindingResolutionContractException($message);
    }

    $this->buildStack[] = $concrete;

    $constructor = $reflector->getConstructor();

    // If there are no constructors, that means there are no dependencies then
    // we can just resolve the instances of the objects right away, without
    // resolving any other types or dependencies out of these containers.
    if (is_null($constructor)) {
        array_pop($this->buildStack);

        return new $concrete;
    }

    $dependencies = $constructor->getParameters();

    // Once we have all the constructor's parameters we can create each of the
    // dependency instances and then use the reflection instances to make a
    // new instance of this class, injecting the created dependencies in.
    $parameters = $this->keyParametersByArgument(
        $dependencies, $parameters
    );

    $instances = $this->getDependencies(
        $dependencies, $parameters
    );

    array_pop($this->buildStack);

    return $reflector->newInstanceArgs($instances);
}

参考地址

  1. stackoverflow whats-the-difference-between-closure-parameters-and-the-use-keyword

  2. php.net anonymous function

Linux 基本操作

显示服务器架构 uname -m | arch

显示服务器cpu核数 nproc

显示服务器cpu详细信息 lscpu | less /proc/cpuinfo

在64位 Centos 7上安装htop

  1. wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
  2. rpm -ihv epel-release-7-9.noarch.rpm
  3. yum install htop

Install Python3.5 and PIP

  1. sudo yum install -y https://centos7.iuscommunity.org/ius-release.rpm
  2. sudo yum update
  3. sudo yum install -y python35u python35u-libs python35u-devel python35u-pip
  4. which -a python3.5
  5. sudo yum install python-pip python-wheel

参考地址

  1. http://www.tecmint.com/install-htop-linux-process-monitoring-for-rhel-centos-fedora/
  2. http://www.codeghar.com/blog/install-latest-python-on-centos-7.html
  3. https://www.cyberciti.biz/faq/linux-get-number-of-cpus-core-command/