嘿,我正在测试PHP中的一些OPP原则
我创建了一个带有一个需要两个参数的方法的类。
当我实例化一个类并使用参数数据调用该方法时,我什么也得不到。
<?php
class example
{
public $request;
public function test($uprn, $sourceChannel)
{
$this->request = new stdClass();
$this->request->uprn = $uprn;
$this->request->sourceChannel = $sourceChannel;
}
}
$test = new example();
$test->test('1', '2');
var_dump($test);die;
我在浏览器中获得的是一个像这样的空对象:
object(example)#1 (0) { }
但我希望如此:
object(example)#1 (2) { ["uprn"]=> string(1) "1" ["sourceChannel"]=> string(1) "2" }
知道我哪里错了......?
答案 0 :(得分:1)
> stdClass is PHP's generic empty class, kind of like Object in Java or object in Python (Edit: but not actually used as universal base class; thanks @Ciaran for pointing this out). It is useful for anonymous objects, dynamic properties.
You can get desired output just like this.
$request = new stdClass();
$request->uprn = $var1;
$request->sourceChannel = $var2;
var_dump($request);die;
please go through this link to understand Generic empty class(stdClass).
http://krisjordan.com/dynamic-properties-in-php-with-stdclass
In PHP OOPS you can get the output as given below
class example
{
var $uprn,$sourceChannel;
public function test($uprn, $sourceChannel)
{
$this->uprn = $uprn;
$this->sourceChannel = $sourceChannel;
}}
$test = new example();
$test->test('1', '2');
var_dump($test);die;
To understand much better go through this
http://php.net/manual/en/language.oop5.php
Thanks