Jump to content

Is it possible to make a $this var global for all classes?


jamesxg1

Recommended Posts

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.

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.