title: How to get the Class property set in another class in PHP
body: <p>I have created two classes <code>User</code> and <code>Post</code>. In the <code>Post</code> class I have created a method <code>getPostUser</code> that should return the <code>User</code> object for the passed <code>postId</code>. With my code, it is setting all property but when I am trying to get the value either from the <code>Post</code> or <code>User</code> object it is giving me the following error.</p>
  
  <blockquote>
    <p>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</p>
  </blockquote>
  
  <p>What I want to know is </p>
  
  <ol>
  <li>Is the way I am writing code correct or wrong?</li>
  <li>How to get the entire User object by passing postId?</li>
  <li>Is there any better way to write such code?</li>
  </ol>
  
  <h1>User Class</h1>
  
  <pre><code>&lt;?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-&gt;userId   = $userId;
          $this-&gt;userName = $userName;
      }
  
      /**
       * @return string
       */
      public function getEmail()
      : string
      {
          return $this-&gt;email;
      }
  
      /**
       * @param string $email
       */
      public function setEmail(string $email)
      : void {
          $this-&gt;email = $email;
      }
  
      /**
       * @return int
       */
      public function getPhone()
      : int
      {
          return $this-&gt;phone;
      }
  
      /**
       * @param int $phone
       */
      public function setPhone(int $phone)
      : void {
          $this-&gt;phone = $phone;
      }
  
  
      /**
       * @return int
       */
      public function getUserId()
      : int
      {
          return $this-&gt;userId;
      }
  
      /**
       * @param int $userId
       */
      public function setUserId(int $userId)
      : void {
          $this-&gt;userId = $userId;
      }
  
      /**
       * @return string
       */
      public function getUserName()
      : string
      {
          return $this-&gt;userName;
      }
  
      /**
       * @param string $userName
       */
      public function setUserName(string $userName)
      : void {
          $this-&gt;userName = $userName;
      }
  
  
  }
  </code></pre>
  
  <h1>Post Class</h1>
  
  <pre><code>&lt;?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-&gt;content  = $content;
          $this-&gt;userId   = $user-&gt;getUserId();
          $this-&gt;userName = $user-&gt;getUserName();
  
          $this-&gt;postId = 10; //db generated postId
  
          return $this;
      }
  
      /**
       * @param int $postId
       *
       * @return \App\User\User
       */
      public function getPostUser(int $postId)
      : User {
  
          if ($this-&gt;postId == $postId) {
              $user = new User($this-&gt;userId, Post::class);
              $user-&gt;setEmail('johndoe@email.com');
              $user-&gt;setPhone(457845412);
  
              return $user;
          }
  
          return NULL;
  
      }
  
      /**
       * @return int
       */
      public function getPostId()
      : int
      {
          return $this-&gt;postId;
      }
  
      /**
       * @param int $postId
       */
      public function setPostId(int $postId)
      : void {
          $this-&gt;postId = $postId;
      }
  
      /**
       * @return int
       */
      public function getUserId()
      : int
      {
          return $this-&gt;userId;
      }
  
      /**
       * @param int $userId
       */
      public function setUserId(int $userId)
      : void {
          $this-&gt;userId = $userId;
      }
  
      /**
       * @return string
       */
      public function getContent()
      : string
      {
          return $this-&gt;content;
      }
  
      /**
       * @param string $content
       */
      public function setContent(string $content)
      : void {
          $this-&gt;content = $content;
      }
  
      /**
       * @return string
       */
      public function getUserName()
      : string
      {
          return $this-&gt;userName;
      }
  
      /**
       * @param string $userName
       */
      public function setUserName(string $userName)
      : void {
          $this-&gt;userName = $userName;
      }
  
  
  }
  </code></pre>
  
  <h1>Index File</h1>
  
  <pre><code>&lt;?php
  
  use App\Post\Post;
  use App\User\User;
  
  require "vendor/autoload.php";
  
  $user = new User(7, 'johndoe');
  $post = new Post();
  
  echo '&lt;pre&gt;', print_r($post-&gt;createPost('This is the hello world post.', $user), TRUE);
  echo $post-&gt;getPostId(), '&lt;br&gt;';
  echo $post-&gt;getContent(), '&lt;br&gt;';
  echo $post-&gt;getUserName(), '&lt;br&gt;';
  echo $post-&gt;getUserId(), '&lt;br&gt;';
  
  
  var_dump($post-&gt;getPostUser(10));
  
  echo $user-&gt;getPhone();
  </code></pre>