Page 1 of 1

Header Classes?

Posted: Wed Dec 05, 2007 3:04 am
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?

Posted: Wed Dec 05, 2007 7:30 am
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.

Re: Header Classes?

Posted: Wed Dec 05, 2007 8:03 am
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.

Posted: Wed Dec 05, 2007 2:39 pm
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"}
 }

Posted: Wed Dec 05, 2007 3:43 pm
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.

Posted: Thu Dec 06, 2007 12:04 am
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

Posted: Thu Dec 06, 2007 2:20 pm
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');