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

  • SEARCH
  • Home
  • 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 8483437
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T20:06:12+00:00 2026-06-10T20:06:12+00:00

I know, globals not (; I am new to OOP, and I’m refactoring some

  • 0

I know, globals not (;

I am new to OOP, and I’m refactoring some functions I created into classes, but I come to a problem. Some of my classes are called from the pages themselves that the users enter (example: $Link->create('page/to/go');). Since this is outside any class, there’s no problem, the links get created.

But then, I have a class that attempts to login the user when created, and if the email entered is not in the database it redirects the user to the register page. Obviously, only doing header ('Location '.$Link->create('page/to/go')) does not work.

What I would do is to set the create method as static and then call it from everywhere. But I think this would be similar to using globals and I am trying to correct bad habits. So how should I do this?

Here’s some of the code for the class Link, implementing a 404-detect that I explained here:

class Link
  {
  private function valid($check)
    {
    $exceptions=array("help/report", "translate");  // More pages and rules to be added
    return in_array($check,$exceptions);
    }

  public function create($arg)
    {
      if (!file_exists("/path/to/".$arg) && !$this->valid($arg))
      {
      // Call a function to store the error in a database.
      error ("404 for ".$arg);

      // One way of handling it. Replace '/' for ' ' and google that string in this page.
      $arg=str_replace("/","%20",$arg);
      return "https://www.google.com/#q=site%3A".Configuration::get('BaseUrl')."%20".$arg;
      }
    else
      {
      // If the page exists or is an exception, create the normal link.
      if(empty($arg)) return Configuration::get('BaseUrl');
      else return Configuration::get('BaseUrl').$arg;
      }
    }
  }

As you can see in the code, when I implement error() into a class I will have a similar problem.

One option I just thought is that I might want to return an error and parse it from outside the __construct() of the User class. But it only works with this, as it’s a yes/not, and I don’t think making a error code up is proper for other cases.

So, what is your suggestion for passing properties and methods from one classes to others? Is it okay to use static for this context?

EDIT. The difficulty of my question it’s that, almost all book, tutorial, page etc I’ve seen talks about how to create a SINGLE class. I haven’t seen any explaining deeply how classes should talk to each other.

EDIT 2. As requested in the comments, here goes some more code. The user accesses his courses entering only the email (getting a level 1), while the user can only edit his settings if he gets a level 2 in the settings page. Not finished as I’ll put some more methods.

class User
  {
  private $Email;
  private $Name;

  public function __construct()
    {
    if (!empty($_POST['logout'])) session_destroy();
    else if ( !empty($_POST['email']) )
      {
      $this->loginEmail($_POST['email']);
      }
    else if ( $_SESSION['level'] == 1 )
      {
      if (!empty($_POST['password']))
        {
        $this->loginFull($_SESSION['email'],$_POST['password']);
        }
      else
        {
        $this->loginEmail($_SESSION['email']);
        }
      }
    else if ( $_SESSION['level'] == 2 )
      {
      $this->loginFull($_SESSION['email'],$_SESSION['pass']);
      }
    else session_destroy();

    }

  private function loginEmail($Email)
    {
    $sql=mysql_query("SELECT * FROM users WHERE email='".mysql_real_escape_string($Email)."'");  //Retrieve the entries from the database
    $row=mysql_fetch_assoc($sql);
    if(mysql_num_rows($sql)==1)
      {
      $this->getData($row);
      $_SESSION['level']=1;
      }
    else header ('Location: http://example.org/new/student/');
    }

  private function loginFull($Email,$Pass)
    {
    $sql=mysql_query("SELECT * FROM users WHERE email='".mysql_real_escape_string($Email)."' AND pass='".md5($Pass)."'");  //Retrieve the entries from the database
    $row=mysql_fetch_assoc($sql);
    if(mysql_num_rows($sql)==1)
      {
      $this->getData($row);
      $_SESSION['pass']=$Pass;
      $_SESSION['level']=2;
      }
    else $this->loginEmail($Email);
    }

  private function getData($row)
    {
    $_SESSION['email']=$row['email'];
    $this->Email=$row['email'];
    $this->Name=$row['name'];
    }

  public function get($Var)
    {
    return $this->$Var;
    }
  }

And now the class Error. As you can see, I already performed some DI without even knowing about it here.

class Error
  {
  private $Page;
  private $Language;
  private $User;

  public function __construct($Page,$Language,$User="None")
    {
    $this->Page=$Page;
    $this->Language=$Language;
    $this->User=$User;

    if (!empty($_REQUEST['banner']))
      $this->Banner=$_REQUEST['banner'];
    }

  public function add($Kind)
    {
    if (!mysql_query("INSERT INTO error (kind, page, lang, user, version, date) VALUES ('".mysql_real_escape_string($Kind)."', '".mysql_real_escape_string($this->Page)."', '".mysql_real_escape_string($this->Language)."', '".mysql_real_escape_string($this->User)."', '".Configuration::get('Version')."',NOW() )"))

    mail(Configuration::get('ErrorEmail'), "Error '".$Kind."' that couldn't be stored.",
       "Full url: ".$FullUrl."\n Language: ".$this->Language->Lang."\n User: ".$Identif."\n Version: ".$Version);  // Inform of the error by email
    }
  }
  • 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-10T20:06:14+00:00Added an answer on June 10, 2026 at 8:06 pm

    Create instance of Link class and pass it to where it is needed (using setter or constructor).
    Definitelly read something abou DI (Dependency Injection) and then DI Containers.

    Nice introduction in Nette Framework – Dependency Injection

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

Sidebar

Related Questions

EDIT I know I'm a new poster here, but I'm not new to StackOverflow.
I know you can not set a key value dynamically, but what about the
Just created a new MVC 4 project and copied over some areas from an
I know this question has been tossed around much but still it's not very
I know it's not perhaps in the true spirit of MVC, but I just
I actually want to create a new local. I know it sounds dubious, but
I'm pretty new to cocoa and Xcode, I've done some basic C coding, but
I know this is not the recommended way of doing it, but if I
I know how to do a global variable, but whenever I try to define
I'm new to object oriented programming and flash. As far as I know, global

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.