You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在多个类之间传递用户登录状态值?

How to Use the isLoggedIn() Method of a User Class in Another Class to Check Login Status

Question

I have a User class structured as follows:

class User {
    private $_data, $_isLoggedIn;
    public function __construct() {
        //Initialising variables
    }
    public function login() {
        //logs in the user and assigns the value true to $_isLoggedIn i.e $this->_isLoggedIn = true
    }
    public function isLoggedIn() {
        return $this->_isLoggedIn;
    }
}

I want to use the return value of the isLoggedIn() method of this class in another class to determine whether the user is logged in. How can I achieve this?


Solution

There are a few clean, practical ways to handle this, depending on how your application is structured:

1. Inject the User Instance (Dependency Injection)

This is the most flexible and testable approach—you pass an existing User object into the class that needs to check login status. It keeps your code loosely coupled and follows best practices like SOLID principles.

class Dashboard {
    private $_user;

    // Inject the User instance via the constructor
    public function __construct(User $user) {
        $this->_user = $user;
    }

    public function showWelcomeMessage() {
        if ($this->_user->isLoggedIn()) {
            echo "Welcome back! You have access to your dashboard.";
        } else {
            echo "Please log in to view your dashboard.";
        }
    }
}

// Usage example:
// First initialize and log in your User
$user = new User();
$user->login(); // This sets $_isLoggedIn to true

// Pass the User instance to the Dashboard class
$dashboard = new Dashboard($user);
$dashboard->showWelcomeMessage();

2. Use a Singleton for Global Access (Simpler Apps)

If you need a single, globally accessible User instance (note: this can make testing harder long-term), adjust your User class to enforce a singleton pattern:

class User {
    private $_data, $_isLoggedIn;
    private static $_instance;

    // Make constructor private to prevent direct instantiation
    private function __construct() {
        // Initialise variables here
    }

    // Get the single instance of User
    public static function getInstance() {
        if (!self::$_instance) {
            self::$_instance = new self();
        }
        return self::$_instance;
    }

    public function login() {
        $this->_isLoggedIn = true;
    }

    public function isLoggedIn() {
        return $this->_isLoggedIn;
    }
}

// Then in your other class:
class Checkout {
    public function processOrder() {
        $user = User::getInstance();
        if ($user->isLoggedIn()) {
            echo "Processing your order...";
        } else {
            echo "Please log in to complete your order.";
        }
    }
}

// Usage example:
$user = User::getInstance();
$user->login();

$checkout = new Checkout();
$checkout->processOrder();

3. Pass the User Instance to a Specific Method

If you don't want to inject the User in the constructor, you can pass it directly to the method that needs to check login status:

class CommentHandler {
    public function submitComment(User $user, $commentText) {
        if ($user->isLoggedIn()) {
            echo "Comment submitted successfully: " . $commentText;
        } else {
            echo "You must be logged in to post comments.";
        }
    }
}

// Usage example:
$user = new User();
$user->login();

$commentHandler = new CommentHandler();
$commentHandler->submitComment($user, "Great article!");

Quick Notes

  • Dependency Injection is the recommended approach for most applications—it makes your code easier to maintain, test, and extend.
  • Singletons work for small, simple projects but can create tight coupling between classes, which becomes a headache as your app grows.

内容的提问来源于stack exchange,提问作者Nomore Mudzvova

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:35:26