给我的课
<?php
declare(strict_types=1);
use Illuminate\Support\Collection;
use stdClass;
class PhpstanIssue
{
/**
* @param Collection<Collection<stdClass>> $collection
*
* @return Collection<Foo>
*/
public function whyDoesThisFail(Collection $collection
我正在使用以下命令创建一个新的stdClass
$usrobj=new stdClass();
它给了我这个错误
PHP Warning: require_once(objects/User/stdClass.php): failed to open stream: No such file or directory in...
似乎php把stdClass当作用户定义的类。
同样的事情也发生在
$db=new mysqli();
我想为现有的CMS编写一个小的加法。为此,我需要从CMS的代码中扩展一个类。
我的代码将在自己的命名空间中编写,而CMS的代码不使用名称空间,这基本上意味着它存在于全局命名空间中。
在代码中,我创建了一个新的stdClass:
$var = new stdClass();
使用这个代码,它总是会产生一个致命的错误:
Fatal error: Class 'MyNamespace\something\StdClass' not found in /some/rather/long/path/to/class.php on line 123
像这样创建stdClass解决了这个问题
$obj = new stdClass();
echo gettype($obj); //object
function abc(object $obj) {
return;
}
abc($obj); //Catchable fatal error: Argument 1 passed to abc() must be an instance of object, instance of stdClass given
为什么调用abc($obj)触发错误?
可捕获的致命错误:传递给abc()的参数1必须是对象的实例,即给定的stdClass实例
class Foo
{
public function bar(): \stdClass
{
$obj = new \stdClass;
$obj->test = 'test';
return $obj;
}
} 在执行$foo = new Foo;时,是否可以注释Phpdoc,以便集成开发环境可以自动完成为$foo->bar()->test
当我将数据从一个函数传递到一个类时,它正在更新我在原始类中传递的数据,即使我没有引用它,我也会遇到问题。
<?php
namespace core\Test\Libraries;
public function hasPurchasedCorrectProducts()
{
$testData = [];
$testData['one'] = new \stdClass();
$testData['one']->qty = 2;
(new \core\Librarie