Global Variables vs. Class Recursion and Overhead - php

I'm building an app based on an existing MVC framework.
The native app bootstraps up by creating a registry object, then populates it with all the classes needed, which is great.
$registry = new \App\Registry;
$loader = new \App\Loader;
$registry->set('load', $loader);
But when a descendant class needs access to the registry or another instantiated object, they're passing in the registry object and setting it as variable in the object of the new class.
$theme = new \App\Theme($registry);
$registry->set('theme', $theme);
Then inside the theme class:
class Theme {
private $registry;
public function __construct($registry) {
$this->registry = $registry;
}
}
This is creating recursion on each instance and by the time the app gets to the front controller class, it's this huge bloated object with massive recursion that's completely unnecessary.
I know that using global variables is frowned upon, and I don't use them unless absolutely needed, but is it not much more efficient to simply call the registry object as a global only where it's needed so as to not have all this bloat?
My solution is to just call the registry as a global within the method in a given class where I need to access an object that's already been registered.
For instance in my example, let's say I need to access the loader object from within a method in Theme.
class Theme {
public function __construct() {
$this->setName();
}
public function setName() {
global $registry;
$loader = $registry->get('load');
$loader->setName();
}
}
This removes the need to set the registry as a variable inside theme, so that each object is only created once.
Is there anything wrong with this approach? Are there security issues? Is there another technique that would produce the result I want without using a global reference?

Related

How to access other classes methods within a class (PHP)

I'm building a PHP OOP MVC Framework for personal use.
I'd like to know if there's a way or the correct implementation of the following:
Being able to call a method from a "subclass" (not extended) to another "subclass". I mean...
I have a class that creates new objects for each subclass instead of using inheritance. Let's call it MainClass, and it has 3 "SubClasses" like this
class MainClass {
public $db;
public $forms;
public $pagination;
function __construct() {
$this->db = new class_db();
$this->forms = new class_forms();
$this->utils = new class_utils();
}
}
And the initialization which is
$MainClass = new MainClass();
I can do for example
$MainClass->utils->show_text("Hello World");
And works fine.
What I'd like to do is... within the $MainClass->utils->test() (Another test method), is to be able to access $MainClass->db without using global or $GLOBALS.
Is there any alternative way of achieving this? To be able to access $MainClass methods and submethods within another submethod (access db from utils and from the main page where MainClass is initialized)? How would it be? I want to be able to access al the submethods, like utils being able to access db and forms method. as well as the pages that are outside MainClass.
Thank you
If utils has to use db, you either have to pass the MainClass instance to utils, so it can call $this->myRefToMain->db, or pass the instance of db itself, so utils can call $this->db. Either way, it cannot (reasonably) crawl up the call stack to find the object that called it.
Object if your class class_utils can exists without MainClass. And its method test() should access some object db of class class_db. This means class_utils depends on class_db and you should inject object of class_db in constructor, for example:
class MainClass {
public $db;
public $forms;
public $pagination;
function __construct() {
$this->db = new class_db();
$this->forms = new class_forms();
$this->utils = new class_utils($this->db);
}
}

Declare a variable created inside of a class as a global variable

How do I make a variable that is created within a class function available outside of that class function?
For instance, the constructor of my main class creates an instance of the logging class. I want this $log variable to be available in other classes (using the global keyword) I would assume.
Is this possible?
You can use static.
class Main
{
static private $log;
public function __construct()
{
self::$log = new Log();
}
static public function getLog()
{
return self::$log;
}
}
Now you can get access to $log everywhere using
Main::getLog();
Using a global would defeat the purpose of OOP. OOP is meant to alleviate dependence and provide code containment for modular use.
You are simply approaching your application design wrong, instead, design ease of access to the logging instance. That is; instantiate your logging class and store the instance somewhere that's accessible (by design) by other classes that depend on it. A sort of "registry".

Instantiation of a PHP class without assignment to a variable

I came a cross this line of code in Codeigniter HMVC extension (by Wiredesignz), where a class got instantiated without getting assigned to a variable (class CI in Base.php)
The code :
class CI extends CI_Controller
{
public static $APP;
public function __construct() {
/* assign the application instance */
self::$APP = $this;
global $LANG, $CFG;
/* re-assign language and config for modules */
if ( ! is_a($LANG, 'MX_Lang')) $LANG = new MX_Lang;
if ( ! is_a($CFG, 'MX_Config')) $CFG = new MX_Config;
parent::__construct();
}
}
/* create the application object */
new CI;
What's the name of this technique?
What's the implication?
This has not a name and the implication is, that the constructor is definitely doing too much. The reason one wants to create an instance of a class without referencing it is, that he only wants the constructor wants to be executed, but nothing more. This means, that the constructor "does" something, but a constructor should only ensure, that an object is in a stable/valid state and nothing more.
In short: Don't assume that this is a good practice. The global and self::$APP = $this confirms my opinion that this is a bad piece of code.
I guess this could be seen as some sort of facade design.
-The Class(constructor is called) and assignment is done, albeit in the constructor itself.
so new CI is just extending the Super object and initializing its own constructor.
Similar to a function, function somefun(){ return }; somefunc();//call somefunc
CI_Controller loads all of the classes required to run Codeigniter, it is the SUPER object
$ci = &get_instance() // CI_Controller

Retrieving data from other classes while extending

First of all I'm very new to OOP and I'm struggling big time. I have a question about the current design of my application and inheritance.
I have a bootstrapper file which initiates all my core classes, after including them, like this:
$security = new Security;
$error_handler = new ErrorHandler;
$application = new Application;
$mysql = new MySQL;
$template = new Template;
$user = new User;
I load the Security and ErrorHandler class first because the Application class needs them first (throw custom 404 errors, make GET variables safe etc). Now all classes extend the Application class, but I can't seem to call any data in any class from a child or parent class.
I read that I need to call the constructor of the parent class first to use any data. That's not really sexy and usefull in my eyes and I don't really see the use of using extends then.
Should I change the design? Or how could I use data from one to another class? I already tried composition but that ended up in a nightmare because I couldn't use any data of different child classes at all.
This is a weird set-up anyhow. You definitely should NOT be using some bootstrapper functionality to preload your classes, especially if certain classes have finite dependencies on other classes. What would be a bit better is this:
Your Security and ErrorHandler classes should use either static methods to allow their functionality to be used without declaring the class OR they should be created as a class var of the Application class.
class Security {
// can be invoked anywhere using Security::somefunction('blah');
public static somefunction($somevar) { ... }
}
OR
require_once('security.php');
require_once('errorhandler.php');
class Application {
public $security;
public $errorHandler;
public function __construct() {
$this->security = new Security;
$this->errorHandler = new ErrorHandler;
}
}
I'm not sure what you mean when you say that you can't access data from any class. Classes should naturally inherit any variables and functions that their parents have declared. So for example:
require_once('application.php');
class User extends Application {
public function throwError($message) {
return $this->errorHandler->somefunction($message);
}
}
Without expressly declaring $this->errorHandler from within the User class, this should still work, as the $errorHandler class var is declared in the Application class.
If you have a child-class that defines a __construct() method, and want its parent's __construct() method to be called, the __construct() method of the child class must call the parent's one.
That's the way it is in PHP ; that's what you must do ; not much of a choice.
As a reference, quoting the Constructors and Destructors section of the manual :
Parent constructors are not called
implicitly if the child class defines
a constructor. In order to run a
parent constructor, a call to
parent::__construct() within the
child constructor is required.

How to avoid using PHP global objects?

I'm currently creating blog system, which I hope to turn into a full CMS in the future.
There are two classes/objects that would be useful to have global access to (the mysqli database connection and a custom class which checks whether a user is logged in).
I am looking for a way to do this without using global objects, and if possible, not passing the objects to each function every time they are called.
You could make the objects Static, then you have access to them anywhere. Example:
myClass::myFunction();
That will work anywhere in the script. You might want to read up on static classes however, and possibly using a Singleton class to create a regular class inside of a static object that can be used anywhere.
Expanded
I think what you are trying to do is very similar to what I do with my DB class.
class myClass
{
static $class = false;
static function get_connection()
{
if(self::$class == false)
{
self::$class = new myClass;
}
return self::$class;
}
// Then create regular class functions.
}
What happens is after you get the connection, using $object = myClass::get_connection(), you will be able to do anything function regularly.
$object = myClass::get_connection();
$object->runClass();
Expanded
Once you do that static declarations, you just have to call get_connection and assign the return value to a variable. Then the rest of the functions can have the same behavior as a class you called with $class = new myClass (because that is what we did). All you are doing is storing the class variable inside a static class.
class myClass
{
static $class = false;
static function get_connection()
{
if(self::$class == false)
{
self::$class = new myClass;
}
return self::$class;
}
// Then create regular class functions.
public function is_logged_in()
{
// This will work
$this->test = "Hi";
echo $this->test;
}
}
$object = myClass::get_connection();
$object->is_logged_in();
You could pass the currently global objects into the constructor.
<?php
class Foo {
protected $m_db;
function __construct($a_db) {
$this->m_db = $a_db;
}
}
?>
I recently revamped my framework in preparation for the second version of our company's CMS. I undid a huge amount of the things I made static in order to replace them with normal objects. In so doing, I created a huge amount of flexibility that used to rely on me going through and hacking into core files. I now only use static constructs when the only alternative is global functions, which is only related to low-level core functionality.
I'm going to show a few lines of my bootstrap.php file (all of my requests get sent through that file, but you can achieve the same result by including it at the top of every file) to show you what I mean. This is an pretty hefty version of what you'd probably use in your situation, but hopefully the idea is helpful. (This is all slightly modified.)
//bootstrap.php
...
// CONSTRUCT APPLICATION
{
$Database = new Databases\Mysql(
Constant::get('DATABASE_HOST'),
Constant::get('DATABASE_USER'),
Constant::get('DATABASE_PASSWORD'),
Constant::get('DATABASE_SCHEMA')
);
$Registry = new Collections\Registry;
$Loader = new Loaders\Base;
$Debugger = new Debuggers\Dummy; // Debuggers\Console to log debugging info to JavaScript console
$Application = new Applications\Base($Database, $Registry, $Loader, $Debugger);
}
...
As you can see, I have all kind of options for creating my application object, which I can provided as an argument in the constructor to other objects to give them access to these "global" necessities.
The database object is self-explanatory. The registry object acts as a container for object I may want to access elsewhere in the application. The loader acts as a utility for loading other resources like template files. And the debugger is there to handle debug output.
I can, for example, change the database class that I instantiate and, voila I have a connection to a SQLite database. I can change the class of the debugger (as noted) and now all of my debug info will be logged to my JavaScript console.
Okay, now back to the issue. How do you give other objects access to all of this? You simply pass it in an argument to the constructor.
// still bootstrap.php
...
// DISPATCH APPLICATION
{
$Router = new Routers\Http($Application);
$Router->routeUri($_SERVER['REQUEST_URI']);
}
...
Not only that, but my Router (or whatever object I construct with it) is more flexible, too. Now I can just instantiate my application object differently, and my Router will behave differently accordingly.
Well, if you already have some object by which you refer to the blog system, you can compose these objects into that, so that they're $blog->db() and $blog->auth() or whatever.

Categories