我试图验证php7函数只接受整数.
这是班级:
<?php
declare(strict_types=1);
class Post
{
private $id;
public function setId(int $id)
{
$this->id = $id;
}
}
Run Code Online (Sandbox Code Playgroud)
这是测试:
<?php
declare(strict_types=1);
class PostTest extends \PHPUnit_Framework_TestCase
{
private function getPostEntity()
{
return new Post();
}
public function testSetId()
{
$valuesExpected = [123, '123a'];
foreach ($valuesExpected as $input) {
$this->getPostEntity()->setId($input);
}
}
}
Run Code Online (Sandbox Code Playgroud)
我得到的错误是:
TypeError: Argument 1 passed to Post::setId() must be of the type integer, string given, called in /path/test/PostTest.php on line 35
有可能验证这样的错误吗?还有,运行这样的支票是否有意义?
我用PHP 7安装了最新的XAMPP服务器(更新:还检查了PHP 7.1)(在我的Windows 10系统上).想要使用opcache,所以我启用了它php.ini.
[opcache]
zend_extension=php_opcache.dll
opcache.enable=1
opcache.enable_cli=0
opcache.memory_consumption=128
opcache.interned_strings_buffer=8
opcache.max_accelerated_files=4000
Run Code Online (Sandbox Code Playgroud)
现在有了这个改变,几乎每个页面都刷新了,我从Apache得到这个错误:
AH00428: Parent: child process 3748 exited with status 3221226356 -- Restarting.
Run Code Online (Sandbox Code Playgroud)
所以,页面正在加载,并且正在加载...等待Apache再次启动.当我关闭opcache(通过设置opcache.enable=0)时,Apache没有重启,一切正常(当然,省略了较慢的Web应用程序主题).
在启用PHP 5.6的XAMPP上加载应用程序时,一切正常opcache.
编辑(添加GIF图像):
正如您所看到的,有时页面刷新应该如此.但有时它会刷新更长时间,而Apache正在重启.
编辑:
说实话,我放弃了这个应用程序,并在Windows上使用PHP(正在使用PHP <= 5.6)大约10年.现在很难/不可能(现在)使PHP 7.x在该操作系统上运行(使用Opcache).决定使用Ubuntu和使用Docker创建的服务器.一切都更容易配置(特别是使用Docker)并且工作得更快.我建议大家也这样做;).
我有一个在PHP 7中定义的varargs类型方法
function selectAll(string $sql, ...$params) { }
Run Code Online (Sandbox Code Playgroud)
我遇到的问题是,当我已经有一个数组时,有时我想调用这个方法,我不能直接将数组变量传递给这个方法.
我正在玩php 7和phpunit 6.这是我写的测试:
<?php declare(strict_types=1);
namespace Test;
use DesignPatterns\Observer\User;
use DesignPatterns\Observer\UserObserver;
use PHPUnit\Framework\TestCase;
class ObserverTest extends TestCase
{
public function testChangeInUserLeadsToUserObserverBeingNotified()
{
$observer = new UserObserver();
$user = new User();
$user->attach($observer);
$user->changeEmail('foo@bar.com');
$this->assertCount(1, $observer->getChangedUsers());
}
}
Run Code Online (Sandbox Code Playgroud)
当我尝试运行此测试时,我收到以下错误消息:
PHP Fatal error: Class 'PHPUnit\Framework\TestCase' not found in /home/.../.../Test/ObserverTest.php on line 9
Run Code Online (Sandbox Code Playgroud)
我用composer安装了PHPUnit,这是我的composer.json文件内容:
{
"require": {
"phpunit/phpunit": "^6.0"
},
"autoload": {
"psr-4": {"DesignPatterns\\": "src/"}
}
}
Run Code Online (Sandbox Code Playgroud)
根据PHPUnit 6文档,您的测试现在应该扩展PHPUnit\Framework\TestCase而不是PHPUnit_Framework_TestCase.
我知道这不是自动加载的问题.实际上,如果我用PHPUnit_Framework_TestCase替换PHPUnit\Framework\TestCase,它工作正常,但我想知道为什么这个语法不起作用.
我尝试了一些谷歌,stackoverflow和PHPUnit的github存储库的研究,但找不到任何东西.
我期待着你的回答,
编辑
这就是我的文件组织方式:
src/
??? DataMapper
? ??? StorageAdapter.php
? ??? …Run Code Online (Sandbox Code Playgroud) 我用php-fpm和cron任务运行php在nginx后面到php二进制文件(/ usr/bin/php).
我发现了一个不一致 - 当我通过php二进制文件和通过fpm运行它时,相同的脚本会输出不同的结果.
注意这仅适用于PHP7.在另一台服务器上,我用5.6测试了它,结果是一样的.
这是我发现的.以下脚本:
<?php
class Test {
public function test(){
$arr = (object) [
'children' => []
];
$arr->children[] = 1;
return $arr;
}
}
$o = new Test();
$o->test();
print_r( $o->test() );
Run Code Online (Sandbox Code Playgroud)
保存到test.php.当我通过浏览器(php-fpm)运行它时,会产生:
stdClass Object
(
[children] => Array
(
[0] => 1
)
)
Run Code Online (Sandbox Code Playgroud)
但是当我从CLI执行它时,结果是不同的:
[root@server1 web]# php -f test.php
stdClass Object
(
[children] => Array
(
[0] => 1
[1] => 1
)
)
Run Code Online (Sandbox Code Playgroud)
没有(对象)强制转换就不会发生这种情况.另外,如果我要实例$arr与new …
在将PHP版本从5.5更改为7.0后,我遇到了Magento 1.8的奇怪行为.这种奇怪的行为是由于工作职能的改变uasort.
源代码:
<?php
$arr = [
"nominal" => [
"before" => ["subtotal", "grand_total"],
"after" => [],
"_code" => "nominal"
],
"subtotal" => [
"after" => ["nominal"],
"before" => ["grand_total", "shipping", "freeshipping", "tax_subtotal", "discount", "tax", "weee"],
"_code" => "subtotal"
],
"shipping" => [
"after" => ["subtotal", "freeshipping", "tax_subtotal", "nominal", "weee"],
"before" => ["grand_total", "discount", "tax_shipping", "tax"],
"_code" => "shipping"
],
"grand_total" => [
"after" => ["subtotal", "nominal", "shipping", "freeshipping", "tax_subtotal", "discount", "tax"],
"before" => [],
"_code" => "grand_total" …Run Code Online (Sandbox Code Playgroud) 我刚刚在我的Laravel项目5.4中添加了修补程序.当我尝试运行时:
php artisan tinker
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
PHP Warning: Uncaught ErrorException: require(/home/abdullah/php_apps/website): failed to open stream: Success in /home/abdullah/php_apps/website/vendor/laravel/tinker/src/ClassAliasAutoloader.php:51
Stack trace:
#0 /home/abdullah/php_apps/website/vendor/laravel/tinker/src/ClassAliasAutoloader.php(51): Illuminate\Foundation\Bootstrap\HandleExceptions->handleError(2, 'require(/home/a...', '/home/abdullah/...', 51, Array)
#1 /home/abdullah/php_apps/website/vendor/laravel/tinker/src/ClassAliasAutoloader.php(51): require()
#2 /home/abdullah/php_apps/website/vendor/laravel/tinker/src/ClassAliasAutoloader.php(33): Laravel\Tinker\ClassAliasAutoloader->__construct(Object(Psy\Shell), '/home/abdullah/...')
#3 /home/abdullah/php_apps/website/vendor/laravel/tinker/src/Console/TinkerCommand.php(59): Laravel\Tinker\ClassAliasAutoloader::register(Object(Psy\Shell), '/home/abdullah/...')
#4 [internal function]: Laravel\Tinker\Console\TinkerCommand->handle()
#5 /home/abdullah/php_apps/website/vendor/laravel/framework/src/Illuminate/Contai in /home/abdullah/php_apps/website/vendor/laravel/tinker/src/ClassAliasAutoloader.php on line 51
PHP Fatal error: Laravel\Tinker\ClassAliasAutoloader::__construct(): Failed opening required '/home/abdullah/php_apps/website' (include_path='.:/home/abdullah/.phpbrew/php/php-7.0.1/lib/php') in /home/abdullah/php_apps/website/vendor/laravel/tinker/src/ClassAliasAutoloader.php on line 51
[Symfony\Component\Debug\Exception\FatalErrorException]
Laravel\Tinker\ClassAliasAutoloader::__construct(): Failed opening required '/home/abdullah/php_apps/website' (include_path='.:/home/abdullah/.phpbrew/php/php-7.0.1/lib/php')
Run Code Online (Sandbox Code Playgroud)
我试过给各种文件夹权限.还尝试将Tinker类添加到console/kernel.php.
我在 Ubuntu 16.04 LTS 上。我正在尝试使用 php7.0 解释器设置 PhpStorm 2018.1。我没有使用 XAMPP。我已经在本地计算机上安装了 Apache Web 服务器和 php7.0。PHP 与 Apache 一起工作得很好。
$ php -v
PHP 7.0.28-0ubuntu0.16.04.1 (cli) ( NTS )
Copyright (c) 1997-2017 The PHP Group
Zend Engine v3.0.0, Copyright (c) 1998-2017 Zend Technologies
with Zend OPcache v7.0.28-0ubuntu0.16.04.1, Copyright (c) 1999-2017, by Zend Technologies
$ which php
/usr/bin/php
Run Code Online (Sandbox Code Playgroud)
我想我应该添加/usr/bin到解释器路径中,但是在我选择文件而不是目录之前,确定按钮是禁用的。
这是显示错误的 PhpStorm 屏幕截图
请告诉我是否需要包含更多信息。因为我不知道还要包括什么。
这让我第二天发疯。我是 Laravel 的新手,并试图让 Laravel 6 在 Google App Engine 标准上工作。尝试:本教程和其他教程,但仍然无法加载 Laravel 索引页面
我做了什么:
创建新的 Laravel 项目
检查 Laravel 是否在本地主机上工作 php artisan serve
生成的新密钥 php artisan key:generate --show
使用以下命令创建 app.yaml 文件:
runtime: php72
env_variables:
APP_KEY: iktbUa2quYPV2av3zDx0XAuEVjwzVQY/oMfyI2PQNKk=
APP_STORAGE: /tmp
VIEW_COMPILED_PATH: /tmp
SESSION_DRIVER: cookie
Run Code Online (Sandbox Code Playgroud)
通过添加修改 bootstrap/app.php $app->useStoragePath(env('APP_STORAGE', base_path() . '/storage'));
冉composer remove --dev beyondcode/laravel-dump-server
还跑了:
php artisan cache:clear,
php artisan route:cache,
php artisan config:clear,
php artisan view:clear(如其他教程所建议的那样)
和 gcloud app deploy
我得到错误:“找不到类‘Facade\Ignition\IgnitionServiceProvider’ ”