鍍金池/ 問答/ PHP問答
維她命 回答

找到你新安裝的phpphp-config這兩個(gè)文件,有可能在/usr/local/opt/php/bin這里,把這兩個(gè)文件鏈接到/usr/bin/目錄下。
執(zhí)行以下命令進(jìn)行鏈接,10.11之后的版本需要使用sudo。(假如你新安裝的php和php-config在/usr/local/opt/php/bin目錄下)

sudo ln -f /usr/local/opt/php/bin/php /usr/bin/php
sudo ln -f /usr/local/opt/php/bin/php-config /usr/bin/php-config

完成后,再運(yùn)行命令php -v查看php版本是不是改變了。

墨染殤 回答

看你問了好幾個(gè) draft-js 的問題,難道你使用之前不看官方文檔嗎?https://draftjs.org/docs/over...

懷中人 回答

thinkphp我知道是能這樣,laravel我不知道能否實(shí)現(xiàn)

還吻 回答

看樣子你用 laravle默認(rèn)的用戶認(rèn)證,在執(zhí)行 php artisan make:auth,它會(huì)生成布局、注冊(cè)和登錄視圖以及所有的認(rèn)證接口的路由。register.blade.php 在 resources.views.auth 里。

或者你可以執(zhí)行 php artisan route:list 找到 | GET|HEAD | register | register| App\Http\Controllers\Auth\RegisterController@showRegistrationForm ,順著控制器找。注意 RegisterController 使用了 trait。不了解 trait你也找不到 showRegistrationForm

落殤 回答

你可以試試阿里云的OSS服務(wù),我一般都是把大資源扔到OSS上這樣自己的服務(wù)器壓力會(huì)減小不少

祉小皓 回答

node不太懂,但我猜可能是這樣的
因?yàn)槌跏枷蛄亢兔罔€都是要跟分塊大小保持一致,也就是說你用cbc-128,那么二者的大小都是16個(gè)字節(jié)
但看你的代碼顯然不是這樣

淚染裳 回答

坑人代碼還原如下

use Event\Event;
use Event\Select;

$pid_list = [];
$parent_pid = posix_getpid();

for ($i = 0; $i < 4; ++$i)
{
    $pair = stream_socket_pair(STREAM_PF_UNIX , STREAM_SOCK_STREAM , STREAM_IPPROTO_IP);
    
    $pid = pcntl_fork();
    
    if ($pid < 0) {
        throw new Exception("創(chuàng)建子進(jìn)程失敗");
    } else if ($pid > 0) {
        // 父進(jìn)程
        fclose($pair[0]);
        
        $child = $pair[1];
        
        fwrite($child , "父進(jìn)程問候 " . posix_getpid());
        
        Select::addIo($child , Event::READ , function($ctrl , $socket , $child) use($parent_pid){
            $msg = fread($socket , 65535);
            
            echo "父進(jìn)程領(lǐng)域!父進(jìn)程 {$parent_pid} 當(dāng)前執(zhí)行進(jìn)程 " . posix_getpid() . " 消息:{$msg}" . PHP_EOL;
        } , $child);
    } else {
        // 子進(jìn)程
        fclose($pair[1]);
        $parent = $pair[0];
        fwrite($parent , "子進(jìn)程問候 " . posix_getpid());
        
        Select::addIo($parent , Event::READ , function($ctrl , $socket , $parent) use($parent_pid){
            $msg = fread($socket , 65535);
            
            echo "子進(jìn)程領(lǐng)域!子進(jìn)程 " . posix_getpid() . " 當(dāng)前執(zhí)行進(jìn)程 " . posix_getpid() . " 消息:{$msg}\n";
        } , $parent);
    }
}

產(chǎn)生的進(jìn)程信息:

父進(jìn)程:32140
子進(jìn)程:32141 32142 32143 32144

坑人的結(jié)果:

父進(jìn)程代碼領(lǐng)域下(子進(jìn)程亂入到父進(jìn)程領(lǐng)域!)

父進(jìn)程領(lǐng)域!父進(jìn)程 32140 當(dāng)前執(zhí)行進(jìn)程 32142 消息 子進(jìn)程問候 32141
父進(jìn)程領(lǐng)域!父進(jìn)程 32140 當(dāng)前執(zhí)行進(jìn)程 32143 消息 子進(jìn)程問候 32142
父進(jìn)程領(lǐng)域!父進(jìn)程 32140 當(dāng)前執(zhí)行進(jìn)程 32144 消息 子進(jìn)程問候 32143
父進(jìn)程領(lǐng)域!父進(jìn)程 32140 當(dāng)前執(zhí)行進(jìn)程 32140 消息 子進(jìn)程問候 32144

子進(jìn)程代碼領(lǐng)域(正確)

子進(jìn)程領(lǐng)域!子進(jìn)程 32141 當(dāng)前執(zhí)行進(jìn)程 32141 消息 父進(jìn)程問候 32140
子進(jìn)程領(lǐng)域!子進(jìn)程 32142 當(dāng)前執(zhí)行進(jìn)程 32142 消息 父進(jìn)程問候 32140
子進(jìn)程領(lǐng)域!子進(jìn)程 32143 當(dāng)前執(zhí)行進(jìn)程 32143 消息 父進(jìn)程問候 32140
子進(jìn)程領(lǐng)域!子進(jìn)程 32144 當(dāng)前執(zhí)行進(jìn)程 32144 消息 父進(jìn)程問候 32140

分析:為什么子進(jìn)程會(huì)調(diào)用在父進(jìn)程定義的事件呢??

這得仔細(xì)分析 for 循環(huán)!

  • 第一次循環(huán),父進(jìn)程向 Event::$events 添加子進(jìn)程監(jiān)聽事件,子進(jìn)程平行執(zhí)行。對(duì)產(chǎn)生的第一個(gè)子進(jìn)程來說, Event::$events 為空,所以沒有拷貝到父進(jìn)程的事件。
  • 第二次循環(huán),父進(jìn)程向 Event::$events 再次添加監(jiān)聽子進(jìn)程事件,此時(shí),對(duì)產(chǎn)生的第二個(gè)子進(jìn)程來說,Event::$events 存在一個(gè)事件!從父進(jìn)程拷貝了一個(gè)事件。
  • 第三次循環(huán),父進(jìn)程再次向 Event::$events 再次添加監(jiān)聽子進(jìn)程事件,此時(shí),對(duì)產(chǎn)生的三個(gè)子進(jìn)程來說, Event::$events 存在兩個(gè)事件!從父進(jìn)程拷貝了兩個(gè)事件。
  • 第四次循環(huán),父進(jìn)程再次向 Event::$evnets 添加事件,此時(shí),相對(duì)產(chǎn)生的第四個(gè)子進(jìn)程來說,Event::$events 存在三個(gè)事件,從父進(jìn)程拷貝了三個(gè)事件。

由上可知,子進(jìn)程實(shí)際也在監(jiān)聽從父進(jìn)程拷貝的事件,如果事件觸發(fā),那么監(jiān)聽該事件的父子進(jìn)程實(shí)際上都會(huì)觸發(fā)。從而出現(xiàn)了令人郁悶的:子進(jìn)程亂入到了父進(jìn)程的領(lǐng)域。

解決方法

在子進(jìn)程中,把從父進(jìn)程拷貝的事件銷毀即可。

for ($i = 0; $i < 4; ++$i)
{
    $pid = pcntl_fork();
    
    if ($pid < 0) {
        throw new Exception("創(chuàng)建子進(jìn)程失敗");
    } else if ($pid > 0) {
        // 父進(jìn)程
        Select::addIo( .... );
        // ...添加相關(guān)事件
    } else {
        // 子進(jìn)程
        // 銷毀從父進(jìn)程拷貝的事件
        Select::clear();
        
        // ....做些什么
    }
}
厭遇 回答

沒有測(cè)過,你試試看

server {
        listen       80;
        listen [::]:80 ipv6only=on;
        server_name  www.example.com;

        root   /data/www/www.example.com;
        index index.php  index.html index.htm;

        location / {
                # 這里使用try_files進(jìn)行url重寫,不用rewrite了。
                try_files $uri $uri/ /index.php?$query_string;
        }

        location ~ \.php($|/) {
            fastcgi_pass   127.0.0.1:9000;
            fastcgi_index  index.php;
            fastcgi_split_path_info ^(.+\.php)(.*)$;
            fastcgi_param   PATH_INFO $fastcgi_path_info;
            fastcgi_param  SCRIPT_FILENAME  $document_root$fastcgi_script_name;
            include        fastcgi_params;
        }

        location ~ /\.ht {
                deny  all;
        }
}

json對(duì)象是類數(shù)組的結(jié)構(gòu), 可以用sizeof函數(shù)判斷長度

$data1=json_decode('{"MCVersion":"v1.10.0","JavaVersion":"v1.8","Server":"Linux"}', true);

var_dump($data1);
echo sizeof($data1);

將會(huì)輸出

array(3) {                                                                                   
  ["MCVersion"]=>                                                                            
  string(7) "v1.10.0"                                                                        
  ["JavaVersion"]=>                                                                          
  string(4) "v1.8"                                                                           
  ["Server"]=>                                                                               
  string(5) "Linux"                                                                          
}                                                                                            
3

注意json_decode第二個(gè)參數(shù)要用true, 將返回array類型,可以用sizeof, 否則將返回stdClass.

巷尾 回答

代碼:

$arr = [
    ['value' => '顏色', 'detailValue' => '', 'attrHidden' => true, 'detail' => ['白色','黑色']],
    ['value' => '包裝', 'detailValue' => '', 'attrHidden' => true, 'detail' => ['大','小']],
    ['value' => '規(guī)則', 'detailValue' => '', 'attrHidden' => true, 'detail' => ['1','2']]
];

function decare($arr){
    $data = [];
    $res = [];
    for ($i=0; $i < count($arr)-1; $i++) { 

        if($i == 0){
            $data = $arr[$i]['detail'];
        }
        //替代變量1
        $rep1 = [];

        foreach ($data as $v) {
            foreach ($arr[$i+1]['detail'] as $g) {
                //替代變量2
                $rep2 = ($i!=0?'':$arr[$i]['value']."_").$v."-".$arr[$i+1]['value']."_".$g;
                $tmp[] = $rep2;
                if($i==count($arr)-2){
                    foreach (explode('-', $rep2) as $k => $h) {
                        //替代變量3
                        $rep3 = explode('_', $h);
                        //替代變量4
                        $rep4['detail'][$rep3[0]] = $rep3[1];
                    }
                    $res[] = $rep4;
                }
            }
        }

        $data = $tmp;
    }
    return [$data,$res];
}
print_r(decare($arr)[1]);

結(jié)果:

clipboard.png

執(zhí)念 回答

首先,你的 $config 數(shù)組中一定包含以下元素:

$config = [
    //others
    'modules' => [
        'debug' => [
            'class' => 'yii\debug\Module',
        ], 
        'gii'   => [
            'class' => 'yii\gii\Module',
        ];
    ],
    //others
]

這里說明一下繼承關(guān)系:

class yii\base\Application extends yii\base\Module

class yii\base\Module extends yii\di\ServiceLocator

class yii\di\ServiceLocator extends yii\base\Component

class yii\base\Component extends yii\base\Object

  • yiibaseApplication::__construct() 方法注解
public function __construct($config = [])
{
    Yii::$app = $this;
    //將\yii\base\Application中的所有的屬性和方法交給Yii::$app->loadedModules數(shù)組中
    static::setInstance($this);

    $this->state = self::STATE_BEGIN;

    //加載配置文件的框架信息 如:設(shè)置別名,設(shè)置框架路徑等等最為重要的是給加載默認(rèn)組件
    $this->preInit($config);

    //加載配置文件中的異常組件
    $this->registerErrorHandler($config);

    // 調(diào)用父類的 __construct。
    // 由于Component類并沒有__construct函數(shù)
    // 這里實(shí)際調(diào)用的是 `yii\base\Object__construct($config)`
    Component::__construct($config);
}

上面方法中 Component::__construct($config) 會(huì)調(diào)用 yii\base\Object::__construct() 方法

  • yiibaseObject::__construct() 方法注解
public function __construct($config = [])
{
    if (!empty($config)) {
        // 將配置文件里面的所有配置信息賦值給Object。
        // 由于Object是大部分類的基類,
        // 實(shí)際上也就是有配置信息賦值給了yii\web\Application的對(duì)象
        Yii::configure($this, $config);
    }
    $this->init();
}

一、下面只是為了說明 'components' => [ 'log' => [...]] 從哪來,若不關(guān)心可以直接看 第二步。

  • 先看 $this->preInit($config);,即 yii\base\Application::preInit(&$config)
public function preInit(&$config)
{
    //others...
    
    // merge core components with custom components
    // 合并核心組件和自定義組件
    foreach ($this->coreComponents() as $id => $component) {
        if (!isset($config['components'][$id])) {
            // 若自定義組件中沒有設(shè)置該核心組件配置信息,直接使用核心組件默認(rèn)配置
            $config['components'][$id] = $component;
        } elseif (is_array($config['components'][$id]) && !isset($config['components'][$id]['class'])) {
            // 若自定義組件有設(shè)置該核心組件配置信息,但是沒有設(shè)置 'class'屬性,則添加該class屬性
            $config['components'][$id]['class'] = $component['class'];
        }
    }
}

/**
 * Returns the configuration of core application components.
 * 返回核心應(yīng)用組件的配置
 * @see set()
 */
public function coreComponents()
{
    return [
        // 日志分配器組件
        'log' => ['class' => 'yii\log\Dispatcher'],
        
        //others...
    ];
}
  • 經(jīng)過 $this->preInit($config);, 我們得到的 $config
$config = [
    'modules' => [
        'debug' => [
            'class' => 'yii\debug\Module',
        ], 
        'gii'   => [
            'class' => 'yii\gii\Module',
        ];
    ],
    'components' => [
        'log'   => [
            'class' => 'yii\\log\\Dispatcher',
        ],
        
        //others...
    ]
    //others...
]


上面只是為了說明 'components' => [ 'log' => [...]] 從哪來

二、重點(diǎn)在這里

  • yii\base\Object::__construct($config = []) 中的 Yii::configure($this, $config);
public static function configure($object, $properties)
{
    // 只是遍歷配置信息,賦值給當(dāng)前對(duì)象
    foreach ($properties as $name => $value) {
        $object->$name = $value;
    }
    return $object;
}
  • 這里我們要配合 yii\base\Object::__set($name, $value)
/**
 * 為實(shí)例不存在的屬性賦值時(shí)調(diào)用
 *
 * Do not call this method directly as it is a PHP magic method that
 * will be implicitly called when executing `$object->property = $value;`.
 * 這個(gè)是PHP的魔術(shù)方法,會(huì)在執(zhí)行 `$object->property = $value;` 的時(shí)候自動(dòng)調(diào)用。
 */
public function __set($name, $value)
{
    // setter函數(shù)的函數(shù)名
    // 由于php中方法名稱不區(qū)分大小寫,所以setproperty() 等價(jià)于 setProperty()
    $setter = 'set' . $name;
    if (method_exists($this, $setter)) {
        // 調(diào)用setter函數(shù)
        $this->$setter($value);
    } elseif (method_exists($this, 'get' . $name)) {
        // 如果只有g(shù)etter沒有setter 則為只讀屬性
        throw new InvalidCallException('Setting read-only property: ' . get_class($this) . '::' . $name);
    } else {
        throw new UnknownPropertyException('Setting unknown property: ' . get_class($this) . '::' . $name);
    }
}

當(dāng)前情景下的 $object 我們可以認(rèn)為是 yii\base\Application 的對(duì)象 $app

  • 當(dāng)遍歷到:
$app->modules =  [
    'debug' => [
        'class' => 'yii\debug\Module',
    ], 
    'gii'   => [
        'class' => 'yii\gii\Module',
    ];
]

這里會(huì)調(diào)用 yii\base\Module::setModules($modules) 方法

public function setModules($modules)
{
    foreach ($modules as $id => $module) {
        $this->_modules[$id] = $module;
    }
}

這樣便有了問題中的

["_modules":"yii\base\Module":private]=>
    array(3) {
        ["debug"]=> object(yii\debug\Module)#33 (28) {
            ......
        }
        ["gii"]=> object(yii\gii\Module)#113 (21) {
            ......
        }
        ...
    }
  • 同樣的道理,當(dāng)遍歷到:
$app->components =  [
    'log'   => [
        'class' => 'yii\\log\\Dispatcher',
    ],
]
  • 這里會(huì)調(diào)用 yii\di\ServiceLocator::setComponents($components) 方法
public function setComponents($components)
{
    foreach ($components as $id => $component) {
        $this->set($id, $component);
    }
}

public function set($id, $definition)
{
    // others ...

    if (is_object($definition) || is_callable($definition, true)) {
        // an object, a class name, or a PHP callable
        $this->_definitions[$id] = $definition;
    } elseif (is_array($definition)) {
        // 定義如果是個(gè)數(shù)組,要確保數(shù)組中具有 class 元素
        // a configuration array
        if (isset($definition['class'])) {
            // 定義的過程,只是寫入了 $_definitions 數(shù)組
            $this->_definitions[$id] = $definition;
        } else {
            throw new InvalidConfigException("The configuration for the \"$id\" component must contain a \"class\" element.");
        }
    } else {
        throw new InvalidConfigException("Unexpected configuration type for the \"$id\" component: " . gettype($definition));
    }
}

這樣便有了問題中的

["_definitions":"yii\di\ServiceLocator":private]=>
        array(24) {
            ["errorHandler"]=> .....
            ["request"]=> ......
            ["log"]=> ......
            ......
        }
我甘愿 回答

原來在thinkphp5下的Model.php實(shí)現(xiàn)了JSON 序列化接口, 調(diào)用了Model.php的toArray方法,所以json_encode才會(huì)輸出這樣的數(shù)據(jù)出來.

實(shí)現(xiàn) JsonSerializable 的類可以在 json_encode() 時(shí)定制他們的 JSON 表示
// JsonSerializable
public function jsonSerialize()
{
    return $this->toArray();
}

參考資料:
JSON 序列化接口

舊酒館 回答

$num = $startotal['startotal']/$selecttotal;
echo is_nan($num) ? 0 : $num;

熟稔 回答
  1. 新開一個(gè)隱藏窗口
  2. 在隱身窗口 訪問 cars.com 》》外國網(wǎng)站
  3. 修改host文件
  4. 在正常窗口(此前沒有訪問過cars.com這個(gè)站點(diǎn)) 》》本地站點(diǎn)
  5. 關(guān)閉隱身窗口,再打開一個(gè)隱身窗口 訪問 cars.com 》》外國站點(diǎn)
  6. 清除緩存后,隱身模式窗口訪問 cars.com 》》外國站點(diǎn)

由此可以猜測(cè)應(yīng)該是瀏覽器的某個(gè)機(jī)制的問題導(dǎo)致了這個(gè)問題

解決方法:可以試一下完全退出瀏覽器程序后再重新打開訪問

墨染殤 回答

攜帶token無效?

樓主CURL構(gòu)建沒有問題,那么建議以下檢查一下

  • 既然是RequestContent-Type,這里是有需求設(shè)置為application/json嗎?其實(shí)不重要哈
  • Authorization 的前綴約束是否一致,可多做嘗試或深讀文檔

相關(guān)擴(kuò)展

當(dāng) nginx 匹配不到任何 server 規(guī)則的時(shí)候,會(huì)默認(rèn)采用第一條 server 配置。

所以,你可以在最前面加一個(gè)空的 server 就可以了。

server {
    listen 80 ;
    server_name _;
    return 403;
}
還吻 回答

Set 什么時(shí)候是不能修改的了 ?

巫婆 回答

圖二 ,服務(wù)器端PHP版本過高,因?yàn)樵赑HP7中已經(jīng)完全移除了mysql_*系列函數(shù),導(dǎo)致函數(shù)不存在錯(cuò)誤。