Can't access/use public class variable?

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:

Can't access/use public class variable?

Post by JAB Creations »

My file system setup...

blog.php includes blog-functions.php and header.php

header.php includes class-settings.php

I can access the $settings class in blog.php but not blog-functions.php, why? :|

Using include spawns a cannot redeclare class error...fine. I tried include_once and that didn't seem to do anything save not attempt to redeclare the class.

Here is the class file which I haven't modified much since originally writing it...

Code: Select all

<?php
class settings
{
 public function set($name,$value) {$this->$name = $value;}
 public function get($name){return $this->$name;}
}
 
 
// Defaults
$contact = '0';
 
     if (isset($_GET['prompt'])) {$prompt = $_GET['prompt'];}
else {$prompt = '0';}
 
 
// Credit to VladSun at devnetwork.net
//$pairs = split('[._]', $_COOKIE['settings']);
//for ($i = 0; $i < count($pairs) / 2; $i++) ${$pairs[$i*2]} = $pairs[$i*2 + 1];
 
 
function example($property, $default_value)
{
 if (isset($_POST['formis']))
 {
  if ($_POST['formis'] == 'siteoptions' && isset($_POST[$property]))
  {
   $post_array = array('audio', 'broadbandimages', 'checkbox',' columns', 'connection', 'css3', 'cursors', 'dhtmleffects', 'initialfocus', 'pagination', 'powerkeys', 'sidebar', 'sounds');
   if (in_array($property,$post_array))
   {
    return $_POST[$property];
   }
   else
   {
    return $default_value;
   }
  }
  else if ($_POST['formis']=='siteoptions' && !isset($_POST[$property]))
  {
   return $default_value;
  }
 }
 else if (isset($_GET[$property]))
 {
  $property = $_GET[$property]; return $property;
 }
 else if (isset($_COOKIE['settings']))
 {
  $pieces = explode('_', $_COOKIE['settings']);
  foreach($pieces as $value)
  {
   $value = explode('.', $value);
   if ($value[0]==$property)
   {
    if ($value[1] != '') {return $value[1];}
    else {return $default_value;}
   }
  }
 }
 else
 {
  return $default_value;
 }
}
 
$cookie_old = array(
'audio'=>'0',
'broadbandimages'=>'0',
'checkbox'=>'0',
'columns'=>'1',
'connection'=>'0',
'css3'=>'0',
'csspatch'=>'1',
'cursors'=>'0',
'dhtmleffects'=>'0',
'dtd'=>'1',
'initialfocus'=>'search_query',
'keyboardlayout'=>'developer',
'mediatype'=>'ns',
'pagination'=>'form',
'personality'=>'0',
'powerkeys'=>'0',
'sidebar'=>'20',
'sounds'=>'0',
'theme'=>'classic'
);
 
$cookie_value;
 
foreach($cookie_old as $value=>$default)
{
 example($value, $default);
 $$value = example($value, $default);
 
 if (isset($cookie_value)) {$cookie_value .= $value.'.'.$$value.'_';}
 else {$cookie_value = $value.'.'.$$value.'_';}
}
 
 
// Set the cookie using the updated values...
//$cookie_value  = 'audio.' . "$audio" . '_broadbandimages.' . "$broadbandimages" . '_browserpatch.' . "$browserpatch" . '_chatroom.' . "$chatroom" . '_connection.' . "$connection" . '_css3.' . "$css3" . '_cursors.' . "$cursors" . '_dhtmleffects.' . "$dhtmleffects" . '_dtd.' . "$dtd" . '_ieccss.' . "$ieccss" . '_initialfocus.' . "$initialfocus" . '_keyboardlayout.' . "$keyboardlayout" . '_mediatype.' . "$mediatype" . '_personality.' . "$personality" . '_powerkeys.' . "$powerkeys" . '_sounds.' . "$sounds" . '_theme.' . "$theme";
if (!headers_sent()) {setcookie('settings',$cookie_value,time()+2592000,'/');}
 
 
//echo $connection;
 
if (isset($_GET['prompt'])) {$initial_focus2 = $_GET['prompt'];}
else {$initial_focus2 = $initialfocus;}
 
// Assign subclasses to true values.
$settings = new settings();
$settings->set('audio',$audio);
$settings->set('broadbandimages',$broadbandimages);
$settings->set('checkbox',$checkbox);
$settings->set('columns',$columns);
$settings->set('connection',$connection);
$settings->set('css3',$css3);
$settings->set('csspatch',$csspatch);
$settings->set('cursors',$cursors);
$settings->set('dhtmleffects',$dhtmleffects);
$settings->set('dtd',$dtd);
$settings->set('initialfocus',$initial_focus2);
$settings->set('keyboardlayout',$keyboardlayout);
$settings->set('mediatype',$mediatype);
$settings->set('pagination',$pagination);
$settings->set('personality',$personality);
$settings->set('powerkeys',$powerkeys);
//$settings->set('scrollbars',$scrollbars);
$settings->set('sidebar',$sidebar);
$settings->set('sounds',$sounds);
$settings->set('theme',$theme);
 
//$settings->set('formis',$_POST['formis']);
 
 
if (!empty($error))
{
 $settings->set('error',$error);
}
else
{
 $error = '0';
 $settings->set('error',$error);
}
?>
I did some reading about class visibility on php.net though it seemed to deal more with class variables and not where the class can be seen itself. I'm not really sure what the problem is that I've run in to so a little insight would be greatly appreciated please. :)
Last edited by Benjamin on Thu May 21, 2009 8:32 am, edited 1 time in total.
Reason: Added spaces to long lines so they wrap.
Post Reply