Getting other class's functions, safest way?
global.php:
PHP Code:
require_once "inc/core.php";
$core = new Core;
require_once "inc/user.php";
$user = new User;
core.php
PHP Code:
class Core
{
function test()
{
return "Hi";
}
}
user.php
PHP Code:
class Core
{
function test()
{
global $core;
return $core->test();
}
}
What is the safest way between, global $core;, $GLOBALS['core']->test(); or function test(&$core){return $core->test();}
I'm sure using global $core; is bad, but what about the others?
Re: Getting other class's functions, safest way?
Question is: why would you use global in a class to call a function of another class? Why not extend the class?
Quoting someone at Stackoverflow regarding global.
Quote:
Originally Posted by Stackoverflow
Because global variables can be modified by the process without other parts of your code knowing it producing unexpected results.
You should always try to keep variables scoped as locally as possible -- not only will this help you with debugging, but it will keep your code easier and cleaner to read and go back and modify.
If you are looking to share data across multiple functions, you might look into making a class for your data and then defining methods to operate on the data encapsulated in your object. (Object-Oriented programming)
Re: Getting other class's functions, safest way?
As Dave touched on, I highly encourage you to stay away from the global scope as much as possible, especially when any data in the global scope may change during runtime (an example of where this should/would not happen is a configuration file accessed globally).
Look into Inheritance (e.g. extend another class) and Dependency Injection for retrieving data in your application or passing it around your application.
Re: Getting other class's functions, safest way?
What about
PHP Code:
User::function();
Re: Getting other class's functions, safest way?
That is calling a static function and will not have access to OOP as it is not referring to an object. It won't solve your issue and is pretty much irrelevant in this case.
What exactly are you trying to do? You have basically just pseudo code here. Well not really, but what I'm trying to say is knowing what you're trying to do in your application will help give you better advice. I have never come across a need to do this in any of my own applications so I am curious as to why you are taking this approach.
You need to either initiate the class again or pass required variables through the class constructor.
Re: Getting other class's functions, safest way?
Quote:
Originally Posted by
Ron
That is calling a static function and will not have access to OOP as it is not referring to an object. It won't solve your issue and is pretty much irrelevant in this case.
What exactly are you trying to do? You have basically just pseudo code here. Well not really, but what I'm trying to say is knowing what you're trying to do in your application will help give you better advice. I have never come across a need to do this in any of my own applications so I am curious as to why you are taking this approach.
You need to either initiate the class again or pass required variables through the class constructor.
You mean something like this?
PHP Code:
class RaGEZONE
{
public $DB;
public function __construct($DB)
{
$this->DB = $DB;
}
public function User2id($id)
{
return $this->DB->prepare(select id from users where name = $id)->result();
}
}
Don't look at the SQL. Just an example.
Re: Getting other class's functions, safest way?
Yes, that is almost exactly how I pass my database classes and have found this to be a secure way to do it.
If you need output from a passed class you can do $value = $this->passedclass->function(arguments); to get the return.
Re: Getting other class's functions, safest way?
But what if you have loads and loads of classes, a lot of work.
Re: Getting other class's functions, safest way?
Make one main-class where you initialize all the classes you need.
PHP Code:
class Clawed
{
public $DB, $User, $Session;
public function __construct()
{
$this->DB = new Database();
$this->User = new User();
$this->Session = new Session();
}
}
PHP Code:
class SomeDAO
{
public $Clawed;
public function __construct($Clawed)
{
$this->Clawed = $Clawed;
}
public function isValidUser($id)
{
return $this->Clawed->DB->prepare(select name from users where id = $id)->num_rows() == 1;
}
}
Can't be that hard
Or
PHP Code:
public function __construct($Clawed)
{
foreach (array('DB', 'User', 'Session') as $Class)
{
$this->$Class = new $Class;
}
}
Actually no idea how efficient it is.
Re: Getting other class's functions, safest way?
Quote:
Originally Posted by
Clawed
But what if you have loads and loads of classes, a lot of work.
If you have "loads and loads of classes" that require lots of functions from other classes, then you're not building your classes correctly.