Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8078879
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:55:56+00:00 2026-06-05T15:55:56+00:00

I have a page class for my web page. When this is called, it

  • 0

I have a page class for my web page. When this is called, it creates a new user. This allows me to check if there is a user logged in. Here is the construct method from the page class:

public function __construct() {
    // check user is logged in
    require 'classes/user.class.php';
    $user = new user();
    if(!$user->isLoggedIn()) {
        // user is not logged in
        // load login page
    }
    else {
        // user is logged in
        // load requested page
    }
}

The whole user validation thing is working just fine, but I now want to access the user object that was created during page construction. Is this a possibility with the way I’m going about things, and if so, how do I access the user object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T15:55:59+00:00Added an answer on June 5, 2026 at 3:55 pm

    To be honest, this a a pretty moot design. Better would be creating the user object outside of the page context then “injecting” it where needed:

    # page.php
    class Page {
        # private $_user; # maybe ... probably not needed.
        public function __construct( User $user ) {
            if ( $user->isLoggedIn() ) { /* etc */ }
            # if this will be needed in other functions, save a copy of the
            # obj reference to as an object attribute:
            $this->_user = $user;
        }
    }
    
    # user.php
    class User {
        public function isLoggedIn() { /* etc */ }
    }
    
    # request_controller.php
    $user = new User();
    $page = new Page( $user );
    

    Basically, avoid initiating classes from inside of object methods. This lead to non maintainable and non testable code. Especially when you’ll need to “mock” the instances with mock services. I’d recommend you rethink the design, the one you have will only get smellier with time.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have created a class called BasePage which inherits System.Web.UI.Page. On this page I've
I have an intermediary class extending System.Web.UI.Page for all of my pages that require
I have a page that is setup like this public partial class _Default :
I have created a web user control called Activity. I have defined a public-facing
I have the following Class which basically creates a list of categories (called commands,
I have to open a page from class extended from javax.swing.AbstractAction class... Is that
I have a page with several tables on it with the same class name.
I currently have a page which is declared as follows: public partial class MyPage
I have the following types and classes: namespace MVC.Models public class Page { public
In C#, I have a class set up like so: class Page { public

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.