PHP类常量似乎总是被解释为字符串(PHP class constants seems to always be interpreted as strings)
类常量似乎总是被解释为字符串,尽管它被定义为整数。 为什么PHP会做这种类型的杂耍,我该如何防止它?
请参阅以下代码:
class BitSet { const NONE = 0; const FOO = 1; const BAR = 2; const ALL = 3; public function __construct( $flags = self::NONE ) { if( $flags & self::ALL !== $flags ) throw new \OutOfRangeException( '$flags = '.$flags.' is out of range' ); $this->value = $flags; } protected $value = self::NONE; } $bs = new BitSet( BitSet::FOO );
最后一行(构造函数的调用)抛出
OutOfRangeException
:PHP Fatal error: Uncaught exception 'OutOfRangeException' with message '$flags = 1 is out of range' in test-case.php:12 Stack trace: #0 /srv/www/matthiasn/class-constant-debug.php(19): BitSet->__construct('1') #1 {main} thrown in /srv/www/matthiasn/class-constant-debug.php on line 12
正如你可以从backtrace条目#0清楚地看到的那样,常量
BitSet::FOO
作为字符而不是整数传递。 因此,位掩码操作$flags & self::ALL !== $flags
不对整数执行,而是在按位ASCII表示上执行,因此失败。我勒个去?! 有没有更好的方法来做到这一点,而不是到处做一个明确的
(int)
广播?A class constant always seems to be interpreted as a string although it is defined as an integer. Why does PHP do this kind of type juggling and how do I prevent it?
See the following code:
class BitSet { const NONE = 0; const FOO = 1; const BAR = 2; const ALL = 3; public function __construct( $flags = self::NONE ) { if( $flags & self::ALL !== $flags ) throw new \OutOfRangeException( '$flags = '.$flags.' is out of range' ); $this->value = $flags; } protected $value = self::NONE; } $bs = new BitSet( BitSet::FOO );
The last line (the invocation of the constructor) throws the
OutOfRangeException
:PHP Fatal error: Uncaught exception 'OutOfRangeException' with message '$flags = 1 is out of range' in test-case.php:12 Stack trace: #0 /srv/www/matthiasn/class-constant-debug.php(19): BitSet->__construct('1') #1 {main} thrown in /srv/www/matthiasn/class-constant-debug.php on line 12
As you can clearly see from backtrace entry #0 the constant
BitSet::FOO
is passed as a character not as an integer. Hence, the bit mask operation$flags & self::ALL !== $flags
is not performed on integers but on the bitwise ASCII representation and therefore fails.What the hell?! Is there any better way to get this right than to do an explicit
(int)
-cast everywhere?
原文:
最满意答案
您可以在表单中包含一个隐藏字段,指示提交的表单。
<form class="form" id="first_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="first_form"> // form data </form> <form class="form" id="second_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="second_form"> // form data </form>
然后在您的路线中检查
which
输入的值。$app->post('/main', function() use ($app) { $which = $app->request()->post('which') if ('first_form' === $which) { // do something } else if ('second_form' === $which){ // do something else } })->name('main.post');
You could include an hidden field in your form which indicates which form was submitted.
<form class="form" id="first_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="first_form"> // form data </form> <form class="form" id="second_form" action="{{ urlFor('main.post') }}" method="post"> <input type="hidden" name="which" value="second_form"> // form data </form>
Then in your route check the value of
which
input.$app->post('/main', function() use ($app) { $which = $app->request()->post('which') if ('first_form' === $which) { // do something } else if ('second_form' === $which){ // do something else } })->name('main.post');
相关问答
更多-
将多个表单序列化在一起?(Serialize multiple forms together?)[2024-02-25]
当你使用jQuery的serialize()函数时,它只是简单地将你的表单变成一个格式为a=1&b=2&c=3的字符串。 所以你当然可以将这个函数应用于两种形式,并将结果与它们之间的&连接起来,并在你的ajax调用中使用结果。 您需要进行一些检查,以确保在进行连接时两个字符串都不为空。 When you use the jQuery serialize() function, it simply turns your form into a string in the format a=1&b=2&c=3. ... -
我这样解决了: $app->get("/",function() use ($app){ if(!isset($_SESSION['group_id'])){ View::login(); // Show login page }else{ $app->redirect('home/'.$_SESSION['group_id']); // Go to home -> This works! } }); $app->get("/home/:group_i ...
-
您需要使用实现ContactApi的Container注册工厂 像这样的东西: $container = $app->getContainer(); $container[App\v1\Api\ContactApi::class] = function ($c) { $apiClient = new App\v1\lib\ApiClient(); return new App\v1\Api\ContactApi($apiClient); } 然后,Slim将使用此工厂在路由之前实例化Con ...
-
问题解决了:我需要重写我的.htaccess到那些子路线。 我所做的就是将我的主htaccess复制到/ api文件夹中 Problem solved: I need to rewrite my .htaccess to that sub-routes. What i did is copy my main htaccess into the /api folder
-
PHP Slimframework CORS(PHP Slimframework CORS)[2024-02-12]
通过向Slim框架middlware添加Access-Control-Allow-Origin , Access-Control-Allow-Methods和Access-Control-Allow-Headers了skype问题。 Resolved via skype by adding Access-Control-Allow-Origin, Access-Control-Allow-Methods and Access-Control-Allow-Headers to Slim framework m ... -
PSR-7决定在Request 值对象中记录使用不可变对象进行请求和响应? Meta文档的一部分。 使用Slim 3,您必须始终从控制器方法返回Response实例。 $app->post('/users', function(ServerRequestInterface $req, ResponseInterface $res) { $data = $req->getParsedBody(); $model = new \Apex\Models\User(ApexDB::getInstan ...
-
SlimFramework v3 - 与AND whithout final'/'相同的路线(SlimFramework v3 - Same route with AND whithout final '/')[2022-06-05]
有两种可能性 指定可选/ $app->get('/admin[/]', function(){ # code here }); 添加中间件,将带有结尾/路由重定向到没有它的URL。 use Psr\Http\Message\RequestInterface as Request; use Psr\Http\Message\ResponseInterface as Response; $app->add(function (Request $request, Response $response, ... -
我最终在我需要的每个页面上使用嵌入式控制器,发布到不同的URL并使用referrer保存cookie。 然后我验证表单,保存带结果的cookie,重定向回引用并呈现结果(错误,谢谢消息......)。 当您处理必须考虑禁用Javascript的场景时,这似乎是最佳选择。 对于用户使用AJAX发布的情况,您可以使用简单条件轻松禁用冗余cookie / flash会话的创建。 I ended up using embedded controller on every page I needed, posting ...
-
您的基本身份验证凭据无法解码为任何有意义的内容。 PHP倾向于默默地忽略它认为格式错误的授权标头。 尝试使用Basic dGVzdDp0ZXN0这样解码为test:test 。 但是,从版本3.5.0开始,已将此解决方法添加到Slim中。 升级Slim安装也可能有所帮助。 Your basic auth credentials do not decode into anything meaningful. PHP tends to silently ignore Authorization headers ...
-
如何使用SlimFramework将多个表单发布到相同的路径?(how to post multiple forms to same route using SlimFramework?)[2022-03-04]
您可以在表单中包含一个隐藏字段,指示提交的表单。