Header Classes?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Header Classes?

Post by JAB Creations »

I'm curious right now (not going to mess with code initially because it's late for me) but I include a header.php file in everything first...of course all my headers are declared in that file. I'm curious if I create a class inside my header file if I can use that in other files that include the header file? For example I currently test for POST, then GET, then COOKIE for several settings on my site...I'd rather set a class or something that I can call universally around my site such as this...

Code: Select all

if ($audio == "1") {}
...where audio is defined in it's own class (by having the arguments stored within the class) so I don't have to repetitiously recreate the same arguments over and over for every single setting. Does this make sense? Am I considering a good approach or is there a better way?
User avatar
Zoxive
Forum Regular
Posts: 974
Joined: Fri Apr 01, 2005 4:37 pm
Location: Bay City, Michigan

Post by Zoxive »

Sounds like you are looking for Singleton's.
http://www.patternsforphp.com/wiki/Singleton wrote: Singletons as we have seen are a useful Design Pattern when used with care. They allow a single instance of any object to be globally available within an application.
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Re: Header Classes?

Post by superdezign »

JAB Creations wrote:Am I considering a good approach or is there a better way?
You're thinking in terms of a sort of config.php file that defines settings and is included in every file. There's nothing wrong with that approach, and it's one of the great features of PHP.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

Locally I have access to config.php but not on any live servers.

Here is a sample of how I would handle an option...I have no clue how I would associate the $css3 variable to the css3 class because that page doesn't actually explain the connection so I'm going to take a shot in the dark. Additionally I couldn't even find "private" on php.net so I have no clue what the heck it is. :roll:

Code: Select all

class css3 (
private $css3 = $css3;

if ($_SERVER['REQUEST_METHOD'] == "POST" && $_POST['formis'] == "siteoptions")
 {
     if (isset($_POST['css3']) && $_POST['css3']=='1') {setcookie('css3','1',time()+2592000,'/'); $css3 = "1"}
 else if (!isset($_POST['css3'])) {setcookie('css3','0',time()+2592000,'/'); $css3 = "0"}
 }
else if ($_SERVER['REQUEST_METHOD'] == "GET" && isset($_GET['css3']))
 {
      if ($_GET['css3'] == "0") {setcookie('css3','0',time()+2592000,'/'); $css3 = "0"}
 else if ($_GET['css3'] == "1") {setcookie('css3','1',time()+2592000,'/'); $css3 = "1"}
 }
else
 {
      if ($_COOKIE['css3'] == "0") {$css3 = "0"}
 else if ($_COOKIE['css3'] == "1") {$css3 = "1"}
 }
User avatar
maliskoleather
Forum Contributor
Posts: 155
Joined: Tue May 15, 2007 2:19 am
Contact:

Post by maliskoleather »

you need to check out http://us2.php.net/manual/en/language.oop5.php, particuarally the visibility section.

classes are essentially libraries of functions... it seems like you're trying to use them entirely different then how you should.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

I've adapted an example and started converting words to something I'd use...though I'm confused. First off would you use a class to handle completely different tasks (like site options (audio, connection, css3, dhtml, etc) and say your database connection) or would you have a single class per individual setting?

Then I'm also confused...everything is defined outside of the class...so then how the heck does the class itself set the definition? I want to use something like $audio or I am guessing I could do something like $setting->audio but it seems excessive compared to $audio? What are the benefits of merging all of my site options in to a single class?

Code: Select all

class setting {
        function bark() {
        print "Grrrrrrr...Woof...Woof!\n<br>";
        }
    }
    class poodle extends setting {
         function bark() {
         print "Yip...Yeep...Yeeeeeeeeeeeeep!\n<br>";
        }
    }
$audio = new setting;
$audio->substringaudio = "audio";
print $audio->substringaudio . ' hello 123';
echo "\n<br>";
$audio->bark(); // function call bark() with class as setting
$connection = new poodle;
$connection->substringaudio = "connection";
print $connection->substringaudio;
echo "\n<br>";
$connection->bark(); // function call bark() with extended class as poodle
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Post by JAB Creations »

I'm getting close I think (big thanks thus far). I had this working when I statically set the $audio variable (which is referenced as $setting->get('audio'))...but I don't want static! So I commented that out for reference and added some conditional code to determine what the correct value should be. Then well, I started getting a lot of errors!

First I can't use private inside of {}...why the heck not? So I decided to create a temporary variable (feels like a waste of time to me) and then assign the $audio variable to equal the value of $thisaudio. It still did not take!

Code: Select all

class setting { 
//private $audio = 0;

function foo()
 {
      if ($_GET['audio'] == "0") {$thisaudio = 0; setcookie('audio','0',time()+2592000,'/');}
 else if ($_GET['audio'] == "1") {$thisaudio = 1; setcookie('audio','1',time()+2592000,'/');}
 else if ($_GET['audio'] == "2") {$thisaudio = 2; setcookie('audio','2',time()+2592000,'/');}
 else if ($_COOKIE['audio'] == "0") {$thisaudio = 0; setcookie('audio','1',time()+2592000,'/');}
 else if ($_COOKIE['audio'] == "1") {$thisaudio = 1; setcookie('audio','1',time()+2592000,'/');}
 else if ($_COOKIE['audio'] == "2") {$thisaudio = 2; setcookie('audio','2',time()+2592000,'/');}
 private $audio = $thisaudio;
 } //foo()

 private $ajax = true; 
 public function get($item){ 
 return $this->$item; 
 } 
} 
//then call it like this 
$setting = new setting(); 
$setting->get('audio');
Post Reply