我出的一组面试题

最近帮忙面试出的一组面试题,个人认为是比较基础的,如果你觉得题目不好, 欢迎提出来。供参考,如果你需要答案也欢迎联系我。

一、 PHP语法篇

  1. 写一个单例类。
  2. C => B => A 三个类,三个类中分别有静态方法m3、m2、m1,在B类的静态方法m中,分别调用C、B、A中的m3、m2、m1方法。
  3. 一个空数组,对应前端的一个对象,json_encode时,将其转换成{},而不是[]
  4. empty函数和isset函数的区别?
  5. 是否用过php7中的??
  6. 对psr规范是否了解,你觉得规范是否有不合理的地方

二、数据库篇

  1. having 和where的区别
  2. dinstinct的含义
  3. MySQL有哪些连接查询

三、Linux服务器篇

  1. 在centos上如何重启php-fpm
  2. 查看PHP安装的扩展,查看是否有安装mbstrig扩展
  3. crontab
  4. 给一个脚本增加所有用户可执行权限
  5. git 从分支A新建分支B,并同时切换到这个分支
  6. git 删除远程分支

Mac带窗口阴影截图

macbook有两个截图快捷键,一个是command+shift+3,用来截整个屏幕图,还有一个command+shift+4自由截图,自由截图时,可以按以下空格键变成窗口截图,窗口截图出来的图片就带有阴影了,默认截图都保存在桌面上(如果你只想截图到粘贴板中,而不是在桌面生成文件,可以尝试:control+cmd+shift+4)。
示例:

cmd+shift+5 区域截图,即默认出来一个区域,自己去调节。

截屏2020-07-17 上午10.58.26

MySQL终端显示当前用户名、服务器、和数据库

MySQL终端默认是这样子的,比较简陋:

➔ mysql
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 150
Server version: 5.6.47-log Homebrew

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.
mysql>

觉得mysql>太简单了,我想让他显示更多的信息,就像平时我会用oh-my-zsh修改本地的shell那样,比如,这是我终端的提示符:

jake in Macintosh at Code/foo-poject on jake-v2.13 [!?$]
➔

可以显示当前用户名,目录,git版本等等信息。

方法很简单,设置一个环境变量即可:

export MYSQL_PS1="\u@\h [\d]> "

再次尝试:

➔ mysql
Warning: Using a password on the command line interface can be insecure.
Welcome to the MySQL monitor.  Commands end with ; or \g.
Your MySQL connection id is 151
Server version: 5.6.47-log Homebrew

Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.

Oracle is a registered trademark of Oracle Corporation and/or its
affiliates. Other names may be trademarks of their respective
owners.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

root@localhost [(none)]> use performancenet;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
root@localhost [performancenet]>

现在提示符部分变得丰富了?,如果想永久生效,需要将export MYSQL_PS1="\u@\h [\d]> "这行命令加入到你的~/.bashrc(或者~/.zshrc)文件中。

SQL中的distinct关键字的正确用法

distinct 并不是一个函数,它只是一个修饰词,它的意思是过滤所有的重复返回行

因此以下语句:

select distinct(user.cellphone), user.points from user;

可能会返回重复的手机号的行,因为distinct 并不是跟(user.cellphone)结合的,它实际上是跟select结合的,将其修正为正确的写法:

select distinct user.cellphone, user.points from user;

这条语句的含义是返回user.cellphoneuser.points结合的唯一行,因此依然会有重复的手机号返回,以下语句则不会有重复的手机号返回,因为返回行总共只有一列:

select distinct user.cellphone from user;

如果想在返回唯一的手机号时,顺便携带points,则应当使用group by:

select user.cellphone, user.points from user group by user.cellphone;

在typora中插入图像

907f9b4f-7fc0-4c00-a9db-e5c984a902e6

这篇文章用来讲述typora中自动上传图片到阿里云oss的过程,需要您会使用php语言,且有开通阿里云oss。

Typora当前版本:

image-20200420123346214

准备自定义上传脚本

mkdir ~/run/alioss -p && cd ~/run/alioss

echo '{
    "name": "jake/alioss",
    "type": "project",
    "authors": [
        {
            "name": "Jaggle",
            "email": "singviy@gmail.com"
        }
    ],
    "require": {
        "aliyuncs/oss-sdk-php": "^2.3"
    }
}' > composer.json

composer install

touch index.php

index.php:

<?php

require_once 'vendor/autoload.php';

$file =  $argv[1];

$name = pathinfo($file, PATHINFO_BASENAME);

$uploader = new \OSS\OssClient(
    '<key>',
    '<sec>',
    'oss-cn-shanghai.aliyuncs.com'
);

$res = $uploader->uploadFile('<bucket>', date('YmdHis') . '-' . rand(10000, 99999) . '-' . $name, $file);

echo "Upload Success:\n";
echo $res['info']['url'] . "\n";
echo $res['info']['url'];

Typrora -> 偏好设置 -> 图像:

image-20200420123449527

最后可以点击验证图片上传选项进行测试。

现在你可以在粘贴和插入图片时,自动上传文件至阿里云oss。

PHP7.*新特性

PHP7.0新特性

  1. 标量类型声明

函数参数除了array,class,interface, callable,目前支持int,string,bool,float标量声明。

<?php
// Coercive mode
function sumOfInts(int ...$ints)
{
    return array_sum($ints);
}

var_dump(sumOfInts(2, '3', 4.1));
  1. 返回值类型声明
<?php
function func(): array {
    return [];
 }
  1. null合并运算符 ??
<?php
// Fetches the value of $_GET['user'] and returns 'nobody'
// if it does not exist.
$username = $_GET['user'] ?? 'nobody';
// This is equivalent to:
$username = isset($_GET['user']) ? $_GET['user'] : 'nobody';
  1. 太空船操作符 <=> 比较大小返回(-1,0,1)

  2. 通过 define() 定义常量数组

  3. 匿名类

  4. 支持 yield from

  5. yield支持return,Generator::getReturn() 获取return的值

PHP7.1新特性

  1. 可为null类型声明
<?php
function testReturn(): ?string {
    return 'elePHPant';
}

function test(?string $name)
{
    var_dump($name);
}
  1. void返回类型声明
  2. 类常量可见性 private const PRIVATE_CONST = 4;
  3. 多异常捕获处理
<?php
try {
    // some code
} catch (FirstException | SecondException $e) {
    // handle first and second exceptions
}
  1. list()支持键名
foreach ($data as list("id" => $id, "name" => $name)) {
    // logic here with $id and $name
}

// [] style
foreach ($data as ["id" => $id, "name" => $name]) {
    // logic here with $id and $name
}

PHP7.2新特性

  1. object对象类型
<?php

function test(object $obj) : object
{
    return new SplQueue();
}

test(new StdClass());
  1. 允许重写抽象方法
<?php

abstract class A
{
    abstract function test(string $s);
}

abstract class B extends A
{
    // overridden - still maintaining contravariance for parameters and covariance for return
    abstract function test($s) : int;
}

PHP7.3新特性

PHP7.4新特性

  1. 属性类型声明
<?php
class User {
    public int $id;
    public string $name;
}
  1. 箭头函数
<?php
$factor = 10;
$nums = array_map(fn($n) => $n * $factor, [1, 2, 3, 4]);
  1. 空合并赋值运算符
<?php
$array['key'] ??= computeDefault();
  1. 数组中间unpack
<?php
$parts = ['apple', 'pear'];
$fruits = ['banana', 'orange', ...$parts, 'watermelon'];
// ['banana', 'orange', 'apple', 'pear', 'watermelon'];
  1. 数字支持_
<?php
$a = 299_792_458;   // decimal

composer 内存溢出的两种解决办法

今天执行composer update命令时,又遇到了composer的经典报错:

Fatal error: Allowed memory size of 1610612736 bytes exhausted (tried to allocate 268435456 bytes) in phar:///usr/local/bin/composer/src/Composer/DependencyResolver/Solver.php on line 223

索性记录一下遇到这个错误的解决办法:

办法1:增加swap内存

mkdir /var/swap && \
dd if=/dev/zero of=/var/swap/swap_1G bs=2048 count=524288 && \
chmod 0600 /var/swap/swap_1G && \
mkswap /var/swap/swap_1G && \
swapon /var/swap/swap_1G && \
echo "/var/swap/swap_1G swap swap default 0 0" >> /etc/fstab
# 检查
free --si -h

对于低配服务器,很有必要哦。

办法2:临时关闭php内存限制

php -d memory_limit=-1 /usr/local/bin/composer update

你甚至可以修改php.ini让memory_limit=-1 永久生效,但是一般不建议这么做。

Laravel 为model触发事件

有时候我们需要在数据

class DemoModel extends Model
{
    protected $table = 'tax_source';

    protected static function boot()
    {
        parent::boot();

        static::creating(function ($model) {
            $model->adminid = auth()->user()->getAuthIdentifier();
            $model->adminname = auth()->user()->user->name;
        });
    }
}

其中,可用的事件有:

saving,saved,updating,updated,creating,created,deleting,deleted。

saving对应的函数为:\Illuminate\Database\Eloquent\Model::saving,其他的类似,参数均为:

/**
 * @prams $model Model 当前操作的model
 */
function ($model) {
}

理论上并不是一定需要在boot函数注册这些事件。