Jump to content

Recommended Posts

I don't nessecarily mean global in scope...but global is use...and global in namespace in the sense the functionality doesn't fall under any context or namespace other than global or system...

For instance, I typically have a function like:

[code]
function redirect($url)
  // TODO: Make sure URL is clean or adjust for mod_rewrite, etc
  header("location: $url");
  exit;
}
[/code]

For security and ease of porting my application over from dynamic URL's to clean SEO URLS...

Another example would maybe be a random string generator...some might argue that is functionality that belongs in a crypto library...I however see this as a global operation, feeling that a crypto lib should probalby provide it's own (some with more entrophy).

So, given the two examples above...I ask...what are some "truely" global operations which you find being reused in your applications...and what is it's purpose?

Cheers :)
Link to comment
https://forums.phpfreaks.com/topic/31443-truely-global-operations/
Share on other sites

I typically use a Global class and lump all of my utility functions in it which I can then call from a static context.  Some examples:

[code]
<?php

GlobalFuncs::importClass($className);
GlobalFuncs::printDebug($dbgString);
GlobalFuncs::redirect($url);
// etc...

?>
[/code]

I might be trampling over some OO purist paradigms here but I find that it's cleaner to put all of my commonly used utilitarian function under one classname which I can use like a namespace throughout my projects.
utexas_pjm has a good start on what I was going to suggest. When you have a major overarching component of your application that you need to use within different areas, I'd recommend seeing if you can define an abstract class to hold all the functions associated with that component. For instance, to combine the example you gave with what utexas_pjm said, I would try to come up with some sort of abstract SEO class that holds all your functions you would need to use [b]for that purpose[/b] throughout your application.
[code]
<?php
abstract Class SEO {
  public function redirect($URL) {
    // TODO: Make sure URL is clean or adjust for mod_rewrite, etc
    header("location: $URL");
    exit();
  }
}

// Then, whenever you need to call it
SEO::redirect($url);
?>
[/code]

This will work with PHP5 only, since PHP4 doesn't fully support OOP and the abstract model.
obsidian,

I'm curious as to why you chose to make the class abstract?  I come from Java background and in Java you use an abstract class to define a class which is too generic to be instanciated but whose methods should be inhertied by all derived classes.  Like the classic example below:

[code]

<?php
abstract class shape {
  private $name;
  public function getArea()
{
    return 0;
}
  public function getName()
  {
    return $this->name;
  }
}

class rectangle extends shape {
private $height;
private $length;
  // ...
public getArea()
{
    return $this->length * $this->height;
}
}
?>

[/code]

Edit: typo
@utexas_pjm - You're actually exactly right. I made another hasty post, and I think I've probably suggested something that is not best coding practice. Thanks for the wake up call. Yes, abstract classes should be intended to be inherited by a more specific class that can then be instantiated within your code. I simply chose to use abstract in this case because, by principle, you can still call the functions (although very generic) through the scope resolution operator and group them together in that way.
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.