Define classes inside of if/else?

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:

Define classes inside of if/else?

Post by JAB Creations »

Is there a way I can set a class within an if/else? It's a rare option and I'd like to reduce server load by encasing it in an if/else...

Code: Select all

$my_class->set('something',$something);
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: Define classes inside of if/else?

Post by Christopher »

Code: Select all

if ($i_want_my_class == true) {
     include 'myclass.php';
}
(#10850)
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Define classes inside of if/else?

Post by JAB Creations »

So I have to create a separate class file? There is no way to do this in a single file?
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Re: Define classes inside of if/else?

Post by onion2k »

Your first post doesn't explain what you're trying to do. If you want to selectively call the set() method, what's wrong with:

Code: Select all

if ($whatever_the_condition_is) {
  $my_class->set('something');
} else {
  $my_class->set($something);
}
 
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Define classes inside of if/else?

Post by JAB Creations »

Echo is PHP's alert for JavaScript and nothing is being echoed so here is more or less what I've tried....

file.php?thing=3

Code: Select all

class my_class {//class stuff here}
 
$settings = new settings();
     if ($_GET['thing'] == "0") {$the_thing = 0;}
else if ($_GET['thing'] == "1") {$the_thing = 1;}
else if ($_GET['thing'] == "2") {$the_thing = 2;}
else if (isset($_GET['thing'])) {$error = thing;}
 
if (isset($error)) {
$settings->set('error',$error);
}
else {
$settings->set('error',$error);
}
...and nothing gets echoed....

Code: Select all

<?php
echo $settings->get('error');
echo $echoerror;
?>
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Define classes inside of if/else?

Post by JAB Creations »

Realization!

I just realized I was doing...
if ($_GET
else if ($_GET
else if ($_GET
else if ($_COOKIE
else if (isset($_GET

I had my procedural programming not correctly set so even if an unexpected get occurred the cookie already existed.

So problem solved. :mrgreen:
Post Reply