在 php 中返回对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 
原文地址: http://stackoverflow.com/questions/2765889/
Warning: these are provided under cc-by-sa 4.0 license.  You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Returning objects in php
提问by user220201
I see similar questions asked but I seem to have problem with more basic stuff than were asked. How to declare a variable in php? My specific problem is I have a function that reads a DB table and returns the record (only one) as an object.
我看到有人问过类似的问题,但我似乎对比被问到的更基本的东西有疑问。如何在php中声明一个变量?我的具体问题是我有一个函数可以读取数据库表并将记录(只有一个)作为对象返回。
class User{
   public $uid;
   public $name;
   public $status;
}
function GetUserInfo($uid)
{
   // Query DB
   $userObj = new User();
   // convert the result into the User object.
   var_dump($userObj);   
   return $userObj;
}
// In another file I call the above function.
....
$newuser = GetUserInfo($uid);
var_dump($newuser);
What is the problem here, I cannot understand. Essentially the var_dump()in the function GetUserInfo()works fine. The var_dump()outside after the call to GetUserInfo()does not work. 
这里有什么问题,我无法理解。本质上var_dump(),函数中的GetUserInfo()工作正常。在var_dump()通话结束后外GetUserInfo()不起作用。
回答by zaf
Using PHP5 it works:
使用 PHP5 它可以工作:
<pre>
<?php
class User{
   public $uid;
   public $name;
   public $status;
}
function GetUserInfo($uid)
{
   $userObj = new User();
   $userObj->uid=$uid;
   $userObj->name='zaf';
   $userObj->status='guru';
   return $userObj;
}
$newuser = GetUserInfo(1);
var_dump($newuser);
?>
</pre>
object(User)#1 (3) {
  ["uid"]=>
  int(1)
  ["name"]=>
  string(3) "zaf"
  ["status"]=>
  string(4) "guru"
}
回答by Ben Fransen
First create a new instance of your Userclass. Then use that instance to call your function and provide the $uid parameter so your query gets executed like it shoudl. If there is a match in your datatable your Userobject will get filled with the DB-results.
首先创建你的User类的一个新实例。然后使用该实例调用您的函数并提供 $uid 参数,以便您的查询像 shoudl 一样执行。如果您的数据表中有匹配项,您的 Userobject 将填充 DB-results。
Personally I prefer using the static calls, it makes your code much more readable and compact.
我个人更喜欢使用静态调用,它使您的代码更具可读性和紧凑性。
Difference:
区别:
$userObj = new User();
$user = $userObj->GetUserInfo('your uid');
Or
或者
$user = User::GetUserInfo('your uid');
And I see a strange }at line 4. Correct me if I'm wrong, but I think it should be after the }from the function GetUserInfo($uid).
我}在第 4 行看到一个奇怪的地方。如果我错了,请纠正我,但我认为它应该在}from 函数之后GetUserInfo($uid)。
回答by Rich R
I'm running into the same issue. However I'm using 5.6.1. Here's what I was doing wrong:
我遇到了同样的问题。但是我使用的是 5.6.1。这是我做错了什么:
class component
{
public function myFunc() 
{
$obj = new SomeComp();
try {
do some work 
return $obj;
} finally {
$obj = null;
}
}
Everything in the class method worked fine, $obj had information when returning. However the results of the following provided a null value:
类方法中的所有内容都运行良好,返回时 $obj 有信息。但是,以下结果提供了一个空值:
$myClass = new component();
$myObj = $myClass->myFunc();
var_dump($myObj);  // returns NULL
Setting $obj = null; in the class method try...finally destroyed $obj making $myObj NULL. Removed the $obj = null; and the method return the proper information.
设置 $obj = null; 在类方法中 try...finally 销毁 $obj 使 $myObj 为 NULL。删除了 $obj = null; 并且该方法返回正确的信息。

