我创建了两个类User
和Post
. 在Post
类中,我创建了一个方法,该方法getPostUser
应该返回User
传递的对象postId
。使用我的代码,它正在设置所有属性,但是当我尝试从Post
orUser
对象获取值时,它给了我以下错误。
致命错误:未捕获错误:在 ...\www\learnphp\src\User\User.php:72 中初始化之前不得访问类型属性 App\User\User::$phone 堆栈跟踪:#0 ...\ www\learnphp\index.php(18): App\User\User->getPhone() #1 {main} 出现在 ...\www\learnphp\src\User\User.php 第 72 行
我想知道的是
- 我编写代码的方式是对还是错?
- 如何通过 postId 获取整个 User 对象?
- 有没有更好的方法来编写这样的代码?
用户类
<?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();