我创建了两个类User
和Post
。在Post
类中,我创建了一个方法getPostUser
,该方法应为传递的User
返回postId
对象。在我的代码中,它设置了所有属性,但是当我尝试从Post
或User
对象获取值时,会出现以下错误。
致命错误:未捕获错误:键入属性App \ User \ User :: $ phone 在初始化之前一定不能访问 ... \ www \ learnphp \ src \ User \ User.php:72堆栈跟踪:#0 ... \ www \ learnphp \ index.php(18):App \ User \ User-> getPhone()#1 {main} 放在第72行的... \ www \ learnphp \ src \ User \ User.php中
我想知道的是
<?php
namespace App\User;
class User
{
/**
* @var int
*/
private int $userId;
/**
* @var string
*/
private string $userName;
/**
* @var string
*/
public string $email;
/**
* @var int
*/
public int $phone;
/**
* User constructor.
*
* @param int $userId
* @param string $userName
*/
public function __construct(int $userId, string $userName)
{
$this->userId = $userId;
$this->userName = $userName;
}
/**
* @return string
*/
public function getEmail()
: string
{
return $this->email;
}
/**
* @param string $email
*/
public function setEmail(string $email)
: void {
$this->email = $email;
}
/**
* @return int
*/
public function getPhone()
: int
{
return $this->phone;
}
/**
* @param int $phone
*/
public function setPhone(int $phone)
: void {
$this->phone = $phone;
}
/**
* @return int
*/
public function getUserId()
: int
{
return $this->userId;
}
/**
* @param int $userId
*/
public function setUserId(int $userId)
: void {
$this->userId = $userId;
}
/**
* @return string
*/
public function getUserName()
: string
{
return $this->userName;
}
/**
* @param string $userName
*/
public function setUserName(string $userName)
: void {
$this->userName = $userName;
}
}
<?php
namespace App\Post;
use App\User\User;
class Post
{
/**
* @var int
*/
private int $postId;
/**
* @var int
*/
private int $userId;
/**
* @var string
*/
private string $userName;
/**
* @var string
*/
private string $content;
/**
* @param string $content
* @param \App\User\User $user
*
* @return \App\Post\Post
*/
public function createPost(string $content, User $user)
: Post {
$this->content = $content;
$this->userId = $user->getUserId();
$this->userName = $user->getUserName();
$this->postId = 10; //db generated postId
return $this;
}
/**
* @param int $postId
*
* @return \App\User\User
*/
public function getPostUser(int $postId)
: User {
if ($this->postId == $postId) {
$user = new User($this->userId, Post::class);
$user->setEmail('johndoe@email.com');
$user->setPhone(457845412);
return $user;
}
return NULL;
}
/**
* @return int
*/
public function getPostId()
: int
{
return $this->postId;
}
/**
* @param int $postId
*/
public function setPostId(int $postId)
: void {
$this->postId = $postId;
}
/**
* @return int
*/
public function getUserId()
: int
{
return $this->userId;
}
/**
* @param int $userId
*/
public function setUserId(int $userId)
: void {
$this->userId = $userId;
}
/**
* @return string
*/
public function getContent()
: string
{
return $this->content;
}
/**
* @param string $content
*/
public function setContent(string $content)
: void {
$this->content = $content;
}
/**
* @return string
*/
public function getUserName()
: string
{
return $this->userName;
}
/**
* @param string $userName
*/
public function setUserName(string $userName)
: void {
$this->userName = $userName;
}
}
<?php
use App\Post\Post;
use App\User\User;
require "vendor/autoload.php";
$user = new User(7, 'johndoe');
$post = new Post();
echo '<pre>', print_r($post->createPost('This is the hello world post.', $user), TRUE);
echo $post->getPostId(), '<br>';
echo $post->getContent(), '<br>';
echo $post->getUserName(), '<br>';
echo $post->getUserId(), '<br>';
var_dump($post->getPostUser(10));
echo $user->getPhone();
答案 0 :(得分:-1)
您收到的错误是因为User类中的$ phone变量已被声明但尚未初始化。当您在User对象上调用getPhone时,其电话号码没有值:
$user = new User(7, 'johndoe');
$post = new Post();
echo '<pre>', print_r($post->createPost('This is the hello world post.', $user), TRUE);
echo $post->getPostId(), '<br>';
echo $post->getContent(), '<br>';
echo $post->getUserName(), '<br>';
echo $post->getUserId(), '<br>';
var_dump($post->getPostUser(10));
echo $user->getPhone(); // phone attribute has not been set
您可以在构造函数中启动它,也可以直接使用默认值的声明内联
public int $phone = 0
如上面的评论中所述,如果所有用户都必须具有电话号码,则应该为带电话的User类定义一个构造函数。
关于您将要通过ID检索用户的意愿,在实际应用程序中,唯一可行的方法是建立一个SQL存储库,并使用SQL查询(或ORM(如果使用的是ORM),通过帖子ID来获取用户)< / p>