/    Sign up×
Community /Pin to ProfileBookmark

Custom Superglobal

I have an instance of a class, how can I make that global without having to define it in all my functions?

Here is an example I have been trying to get to work:
My Class file:

[code=php]<?php
class phpLive{
// Some methods and properties here
}
$phplive = $_live = $live = $_ = new phpLive();
global $live;
?>[/code]

The Page:

[code=php]<?php
require_once ‘phpLive.php’;
$live->get_http(“http://google.com”)->callback(function(){
echo $live->title;
});
?>[/code]

But the above code gives me this error:

[QUOTE]

Notice: Undefined variable: live in C:wampwwwphpLiveindex.php on line 13

Notice: Trying to get property of non-object in C:wampwwwphpLiveindex.php on line 13

[/QUOTE]

I don’t want to have to place “global $live” inside each function, so is there a way for me to access it?

BTW I have tried these as well without success:

[code=php]echo $this->title;
echo self::title;[/code]

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogAug 03.2011 — Well, you could always use the $GLOBALS array, I suppose. However, any such use of global variables is counter to the spirit and intent of OOP, and I'd rather see you passing the object as a parameter to the functions that need to use it, or as a compromise creating a singleton Registry object that could contain the object in question and then be accessed as/when needed.
Copy linkTweet thisAlerts:
@The_Little_GuyauthorAug 03.2011 — I would like to have it readily available as soon as I make the new Closure function (or what ever it is called).

So people who use this library don't have to make the instance global within the function, like this:
[code=php]<?php
require_once 'phpLive.php';
$live->get_http("http://google.com")->callback(function(){
global $live;
echo $live->title;
});
?>[/code]
Copy linkTweet thisAlerts:
@NogDogAug 03.2011 — It's not really clear to me what you want to do, but maybe you can do:
[code=php]
<?php
require_once 'phpLive.php';
$live->get_http("http://google.com")->callback(function($live){
echo $live->title;
});
?>
[/code]
Copy linkTweet thisAlerts:
@The_Little_GuyauthorAug 04.2011 — Okay, in jquery $ is created in the jquery.js file, when you call $ within a function you don't need to make it global because it already is.

example:
[CODE]$("a").click(function(){
$.(this).addClass("newClass");
});[/CODE]


in the above code, I didn't have to call $ and say "use the global instance" it just knows to use the instance of the jquery class. I also didn't have to pass it as a parameter to be able to use the class either. I would like to add similar functionality but in a php way, the same as how $_POST, $_GET, $_SESSION, etc. are used, where you don't have to do this to use them:

[code=php]function myFunc(){
global $_SESSION;
}[/code]


You don't need to do that with $_SESSION, I want to do the same thing with my class "$live". Does that clear anything up? If so, do you know a way for me to do that?
Copy linkTweet thisAlerts:
@NogDogAug 04.2011 — The only way you can do that in PHP (which is not JavaScript ? ) is by using one of the built-in super-global arrays, with $GLOBALS seeming to be the logical candidate. You should not even have to explicitly set it, e.g.:
[code=php]
<?php
class Bar
{
public $greeting;
}

function foo()
{
if(
isset($GLOBALS['bar']) and
is_object($GLOBALS['bar']) and
is_a($GLOBALS['bar'], 'Bar')
) {
echo $GLOBALS['bar']->greeting;
}
else {
user_error('$bar instance of Bar does not exist');
}
}

$bar = new Bar();
$bar->greeting = "Hello, World.";
foo();
?>
[/code]

But I still don't like it. ?
Copy linkTweet thisAlerts:
@The_Little_GuyauthorAug 04.2011 — hmm... that isn't really going to help me ?

It seems as if this is the best I am going to get:
[code=php]<?php
require_once 'phpLive.php';
$live->get_http("http://google.com")->callback(function(){
global $live;
echo $live->title;
});
?>[/code]


I wish I didn't have to define that in my function. I want to use $live as a global, I wish I could just use it as a global just like jquery does ?

If you have any other ideas, I am willing to hear.

Thanks!
Copy linkTweet thisAlerts:
@NogDogAug 04.2011 — How about the use() syntax?
[code=php]
<?php
require_once 'phpLive.php';
$live->get_http("http://google.com")->callback(function($live) use($live) {
echo $live->title;
});
?>
[/code]

(I don't have a 5.3+ host available to test it on at the moment.)
Copy linkTweet thisAlerts:
@The_Little_GuyauthorAug 04.2011 — use() does work, but it is still not what I am after.
[code=php] <?php
$live->get_http("http://godaddy.com")->callback(function() use($live){
echo "<h2>$live->title</h2>";
});
?>[/code]
Copy linkTweet thisAlerts:
@NogDogAug 04.2011 — Well, unless I'm missing something, I don't think what you're after exists in PHP. If you are dead set on using a PHP anonymous function in that situation, then using the use() functionality that is part of anonymous functions seems like a logical solution.* It would also allow you to use type-hinting in the function parameter list (instead of that moderately complex if clause I used earlier). If your goal is simply to imitate JavaScript and save yourself a tiny bit of typing, well, that's probably not a real good reason for application design in PHP or any other language. ?

___________
  • * Although no such solution will be backward-compatible with any pre-5.3.0 release of PHP, mind you.
  • Copy linkTweet thisAlerts:
    @The_Little_GuyauthorAug 04.2011 — If your goal is simply to imitate JavaScript and save yourself a tiny bit of typing, well, that's probably not a real good reason for application design in PHP or any other language. ?[/QUOTE]

    I just wanted to make it faster to use my library. The goal of the library is the same as jquery's goal, "Write less. Do more."
    Copy linkTweet thisAlerts:
    @NoasITAug 04.2011 — The difference here is, everything you write for jquery gets sent to the browser and interpreted client side. Everything in php is processed ahead of time, therefore you aren't worried about transferring TONS of code, because its processed before hand, or overloading a client who has a POS pc because it can't handle all the memory needed.
    ×

    Success!

    Help @The_Little_Guy spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 5.19,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @AriseFacilitySolutions09,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,

    tipper: @darkwebsites540,
    tipped: article
    amount: 10 SATS,
    )...