jamesxg1 Posted February 2, 2011 Share Posted February 2, 2011 Hiya! I was wondering if this is possible. Here is my example. This is a example class; <?php class a { public function __construct() { } public function returnValue() { include 'files/lang.php'; $this->lang = new lang(); return $this->lang->displayLang('10'); } } ?> This is the second class file; <?php class b { public function __construct() { } public function returnValue() { include 'files/lang.php'; $this->lang = new lang(); return $this->lang->displayLang('10'); } } ?> This is the main file; <?php include 'test/a.php'; $test = new a(); $test->returnValue(); include 'test/a.php'; $test2 = new b(); $test2->returnValue(); ?> With this I'm getting an error stating I cannot redeclare the function lang. I have found a way around this by using require_once but I was wondering if remove the include and $this->lang var from class 2 and I put this as the $this->lang var declaration in class 1; global $this->lang = new lang(); will class 1 and class 2 be able to use it from only the one declaration made in class 1. If so, would this be the way to do it or is there a better/working way to do it? Many many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/226456-is-it-possible-to-make-a-this-var-global-for-all-classes/ Share on other sites More sharing options...
jamesxg1 Posted February 2, 2011 Author Share Posted February 2, 2011 BUMP. Quote Link to comment https://forums.phpfreaks.com/topic/226456-is-it-possible-to-make-a-this-var-global-for-all-classes/#findComment-1168919 Share on other sites More sharing options...
jamesxg1 Posted February 2, 2011 Author Share Posted February 2, 2011 I'm going to take a guess by saying this is not possible then? LOL. Many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/226456-is-it-possible-to-make-a-this-var-global-for-all-classes/#findComment-1168963 Share on other sites More sharing options...
KevinM1 Posted February 2, 2011 Share Posted February 2, 2011 Your example is hardly clear.... Quote Link to comment https://forums.phpfreaks.com/topic/226456-is-it-possible-to-make-a-this-var-global-for-all-classes/#findComment-1169015 Share on other sites More sharing options...
jamesxg1 Posted February 3, 2011 Author Share Posted February 3, 2011 Your example is hardly clear.... Sorry, I will try to explain in more detail. a.php <?php class a { public function __construct() { } public function returnValue() { return $global->displayLang('1'); /* This is taken from the index.php page, this is what I need to know if is possible? I used the var $global to show that this is what I am hopefully trying to make global */ } } ?> b.php <?php class b { public function __construct() { } public function returnValue() { return $global->displayLang('2'); /* This is taken from the index.php page, this is what I need to know if is possible? I used the var $global to show that this is what I am hopefully trying to make global */ } } ?> lang.php <?php class lang { private $id; public function __construct() { } public function displayLang($id) { return 'We got the ID: ' . $id . '.'; } } ?> index.php <?php include 'lang.php'; include 'a.php'; include 'b.php'; $global = new lang(); /* Can another class use this? */ $a = new a(); echo $a->returnValue(); $b = new b(); echo $b->returnValue(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/226456-is-it-possible-to-make-a-this-var-global-for-all-classes/#findComment-1169322 Share on other sites More sharing options...
trq Posted February 3, 2011 Share Posted February 3, 2011 Seriously, your not making allot of sense. If you include the same file twice and that file contains function or class definitions you WILL get errors because you cannot redeclare functions or classes. As for this.... <?php include 'lang.php'; include 'a.php'; include 'b.php'; $global = new lang(); /* Can another class use this? */ $a = new a(); echo $a->returnValue(); $b = new b(); echo $b->returnValue(); ?> Yes. Pass $global into the __construct of the object that need's it. I would however make sure you define a known interface firstly. Don't even mention globals and class / objects together. Quote Link to comment https://forums.phpfreaks.com/topic/226456-is-it-possible-to-make-a-this-var-global-for-all-classes/#findComment-1169339 Share on other sites More sharing options...
jamesxg1 Posted February 3, 2011 Author Share Posted February 3, 2011 Seriously, your not making allot of sense. If you include the same file twice and that file contains function or class definitions you WILL get errors because you cannot redeclare functions or classes. As for this.... <?php include 'lang.php'; include 'a.php'; include 'b.php'; $global = new lang(); /* Can another class use this? */ $a = new a(); echo $a->returnValue(); $b = new b(); echo $b->returnValue(); ?> Yes. Pass $global into the __construct of the object that need's it. I would however make sure you define a known interface firstly. Don't even mention globals and class / objects together. I don't really understand how to explain it in another way. The redeclaration error is the reason why I am trying to do this, I did find a solution "require_once" but this is acting as more of a bandage to the issue rather than a fix or solution. $global is a referral to a function held in another class and the values of displayLang() will always be different, if I pass $global into the constructor of the class that is in need of it (every class I have built (50+)) as an object, will I still have the same results as this? <?php session_start(); class test { public function __construct() { require_once '../lang/lang.php'; $this->lang = new lang(); return $this->lang->displayContent('21'); } } ?> As for the globals I mentioned, do not fear LOL! I am not that stupid LOL! That would be the biggest mistake possible! What I meant by globals is to make a var "global to all classes that need it", so to simplify things I need a way to be able to call upon a function from any class without having to include it in said class again. I want to be able to include it in the index.php file once and have it so that any other class I include on index.php can call upon the function when needed. If that makes sense? Many many thanks, James. Quote Link to comment https://forums.phpfreaks.com/topic/226456-is-it-possible-to-make-a-this-var-global-for-all-classes/#findComment-1169361 Share on other sites More sharing options...
trq Posted February 3, 2011 Share Posted February 3, 2011 I need a way to be able to call upon a function from any class without having to include it in said class again. The best way you can do that is to pass one object into the another. Quote Link to comment https://forums.phpfreaks.com/topic/226456-is-it-possible-to-make-a-this-var-global-for-all-classes/#findComment-1169580 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.