php - access class private property through getter method

php
voted: 0
vote
php getter private property

access class privvate property through getter method

<?php
     
    class User{
        public $id;
        public $name;
     
        public function  __construct(){
        }
    }
     
    class UserForm{
        private $_user;
        public function getUser() {
            if ($this->_user == null) {
                $this->_user = new User;
            }
            return $this->_user;
        }
        public function save(){
            $this->user->id = 1;
            $this->user->name = 'jessica';
            echo 'user: ' . $this->user->id . '. ' . $this->user->name . ' saved succesfully to database';
        }
     
    }
    $userForm = new UserForm();
    $userForm->save();
   // output: user: 1. jessica saved succesfully to database

    ?>
working url: https://ideone.com/TdBUwZ
version: not entered
Leave a Comment:
Characters left:
No comments yet.