I have created two classes User
and Post
. In the Post
class I have created a method getPostUser
that should return the User
object for the passed postId
. With my code, it is setting all property but when I am trying to get the value either from the Post
or User
object it is giving me the following error.
Fatal error: Uncaught Error: Typed property App\User\User::$phone must not be accessed before initialization in ...\www\learnphp\src\User\User.php:72 Stack trace: #0 ...\www\learnphp\index.php(18): App\User\User->getPhone() #1 {main} hrown in ...\www\learnphp\src\User\User.php on line 72
What I want to know is
- Is the way I am writing code correct or wrong?
- How to get the entire User object by passing postId?
- Is there any better way to write such code?
User Class
<?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;
}
}
Post Class
<?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;
}
}
Index File
<?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();