我很困惑PHP中的"静态"和"动态"函数和对象如何协同工作,特别是关于__callStatic().
__callStatic()的工作原理如下:
你可以有一个普通的类MyClass,你可以在类中放置一个名为__callStatic()的静态函数,只有当MyClass没有你想要的名字的静态函数时才会调用它.
即我打电话
MyClass::newFunction();
newFunction()被静态调用,但MyClass没有声明它.所以,然后__callStatic()被调用,你可以说Run Code Online (Sandbox Code Playgroud)$myObject=new SomeOtherClass(); $myObject->newFunction();它调用你想要的函数但是在其他一些对象上.
精简版:
换句话说,__ callStatic()执行此操作:
MyClass::newFunction();
Run Code Online (Sandbox Code Playgroud)
这隐藏了这个:
(new SomeOtherClass())->newFunction();
Run Code Online (Sandbox Code Playgroud)
说什么呢?看起来代码从类中调用静态函数看起来就是从其他类调用该函数并通过实例化调用它,而不是静态调用.
请解释一下!
为什么这样做?你可以在其他地方做这样的事情,比如C++或Java吗?我正在寻找关于语言中静态和动态函数的简短而简洁但有用的解释,在这种情况下,无论是__callStatic()violates还是conforms语言结构的大图.或者它完全是一种新的语言结构.
我已经使用PHP了很长一段时间了,但这对我来说总是一个谜,在变量前正确使用感叹号(负号).
有什么!$var表明?是var false,空,没有设置等?
以下是我需要学习的一些例子......
例1:
$string = 'hello';
$hello = (!empty($string)) ? $string : '';
if (!$hello)
{
die('Variable hello is empty');
}
Run Code Online (Sandbox Code Playgroud)
这个例子有效吗?如果$string是空的话,if语句真的可以工作吗?
例2:
$int = 5;
$count = (!empty($int)) ? $int : 0;
// Note the positive check here
if ($count)
{
die('Variable count was not empty');
}
Run Code Online (Sandbox Code Playgroud)
这个例子有效吗?
我从不使用上述任何示例,我将这些限制if ($var)为仅具有布尔值的变量.我只需要知道这些例子是否有效,这样我就可以扩大对if ($var)语句的使用范围.他们看起来很干净.
谢谢.
来自http://php.net/manual/en/mysqli.quickstart.prepared-statements.php的正确MySQLi参数化查询语法:
$stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (?)");
$stmt->bind_param("i", $id);
Run Code Online (Sandbox Code Playgroud)
但绝不是这样的:
$stmt = $mysqli->prepare("INSERT INTO test(id) VALUES (:id_value)");
$stmt->bind_param("i", "id_value", $id);
Run Code Online (Sandbox Code Playgroud)
在我看来,named parameter替换是在API级别实现的合理特性.我很惊讶MySQLi只unnamed parameters在库中实现.
有正当理由吗?看看PDO,DQL,ORM如何在查询中采用命名参数对我来说没有意义.
我希望MySQLi开发人员不是"我们很懒,不想"的情况.我相信一定有充分的理由,我正在寻找这个理由,或者寻找理由的方法.MySQLi扩展库中未实现命名参数的原因.
我运行了doctrine控制台工具:
$ php vendor/doctrine/orm/bin/doctrine orm:schema-tool:create --dump-sql
Run Code Online (Sandbox Code Playgroud)
我得到了这个而不是预期的功能:
You are missing a "cli-config.php" or "config/cli-config.php" file in your
project, which is required to get the Doctrine Console working. You can use the
following sample as a template:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
// replace with file to your own project bootstrap
require_once 'bootstrap.php';
// replace with mechanism to retrieve EntityManager in your app
$entityManager = GetEntityManager();
return ConsoleRunner::createHelperSet($entityManager);
Run Code Online (Sandbox Code Playgroud)
问题:
bootstrap.phpGetEntityManager我该如何工作?
问题 - 似乎我不能将两个表单嵌入到单个表中并使我的HTML验证.例如,W3 Validator告诉我,Element <form> not allowed as child of element <tr> in this context. 我没有办法绕过验证错误.
我想要实现的是:
示例UI
数字是输入字段,保存/删除是按钮.

我在下面简化了不符合要求的HTML:
<!DOCTYPE html>
<html>
<head>
<title>HTML</title>
</head>
<body>
<table>
<tr>
<form>
<td><input type="text" name="row_id" value="1"></td>
<td><input type="text" name="five" value="5"></td>
<td><input type="text" name="three" value="3"></td>
<td><input type="submit" value="Save This Row"></td>
</form>
<td><form>
<input type="text" name="row_id" value="1">
<input type="submit" value="Delete This Row">
</form></td>
</tr>
</table>
</body>
</html>
Run Code Online (Sandbox Code Playgroud)
HTML工作令人惊讶,但它没有验证.我正在寻求一个解决方案,它同时做到了 - 工作和验证.
在我的composer.json配置文件中,我有:
"require": {
"zendframework/zend-log" : "~2.3",
},
"require-dev": {
"phpunit/phpunit": "^5.4"
}
Run Code Online (Sandbox Code Playgroud)
我想要:
"require": {
"zendframework/zend-log" : "^2.9",
},
"require-dev": {
"phpunit/phpunit": "^6.2"
}
Run Code Online (Sandbox Code Playgroud)
注意版本号更改
怎么样?我希望它能够自动完成,而不必查找每个可用的最新版本并手动进行编辑。
有这个问题,但无济于事:尝试使作曲家获取最新的软件包版本时,如何解决未找到软件包的错误?
我有一个这种格式的字符串 /abcd-efg-sms-3-topper-2
我想从中删除第一个/字符.
我知道我可以使用substr()函数删除它,但我不想使用它,因为我首先要检查第一个字符是否为斜杠.有没有其他方法我可以删除斜杠,而不首先检查斜杠,并有这种方式合理的性能(即避免复杂的常规表达)?
我正在使用BjyAuthorize和Zend Framework2来实现授权,并且能够成功地集成来自数据库的角色.现在我想从数据库表中获取规则和保护.我怎样才能做到这一点?
我有一个名为Assembly隐藏底层产品实现的类. ProductA可以有一套吸气剂,ProductB可以有另一套吸气剂.
虽然PHP非常宽容,如果我不混淆产品和吸气剂,我的代码会起作用,但是当我搞砸并填充Assembly时ProductA,没有提供保护,但是后来使用了getter ProductB.
此外,我的IDE确实知道来自任何一个类的方法,因此当我Assembly使用其中任何一个的getter 工作时,没有提供自动完成Product.
我想要更多的防弹代码,一个Assembly 知道或知道Product它当前托管哪个子类型的代码.有办法吗?
以下示例
class Assembly
{
private $product;
public function setProduct(Product $product) {
$this->product = $product;
}
public function getA() {
return $this->product->getA();
}
public function getB() {
return $this->product->getB();
}
}
class Product { }
class ProductA extends Product
{
public function getA() {
return "A";
}
}
class ProductB extends Product
{
public …Run Code Online (Sandbox Code Playgroud) 在浏览器上加载典型页面时,我收到此消息:
不推荐使用突变事件。请改用 MutationObserver。
抛出此消息的行号如下: jquery-3.2.1.js:5062:6
查看 jQuery 源代码,这是它包含的代码,其中有问题的行是elem.addEventListener( type, eventHandle );
// Init the event handler queue if we're the first
if ( !( handlers = events[ type ] ) ) {
handlers = events[ type ] = [];
handlers.delegateCount = 0;
// Only use addEventListener if the special events handler returns false
if ( !special.setup ||
special.setup.call( elem, data, namespaces, eventHandle ) === false ) {
if ( elem.addEventListener ) {
elem.addEventListener( type, eventHandle );
# ^^^^^ the …Run Code Online (Sandbox Code Playgroud) php ×5
doctrine ×2
bjyauthorize ×1
composer-php ×1
console ×1
doctrine-orm ×1
firefox ×1
forms ×1
html ×1
html-table ×1
if-statement ×1
javascript ×1
jquery ×1
mysqli ×1
negation ×1
oop ×1
performance ×1
polymorphism ×1
static ×1
string ×1
trim ×1
variables ×1
zend-db ×1