views:

1315

answers:

2

Below code works fine:

<?php session_start();

   $_SESSION['color'] = 'blue'; 

   class utilities
   {
            public static $color;

        function display()
            {
             echo utilities::$color = $_SESSION['color'];
            }

   }
   utilities::display(); ?>

This is what I want but doesn't work:

<?php session_start();

$_SESSION['color'] = 'blue'; 

class utilities  {  
     public static $color = $_SESSION['color']; //see here

     function display()     
     {   
         echo utilities::$color;    
     }   } utilities::display(); ?>

I get this error: Parse error: syntax error, unexpected T_VARIABLE in C:\Inetpub\vhosts\morsemfgco.com\httpdocs\secure2\scrap\class.php on line 7

Php doesn't like session variables being stored outside of functions. Why? Is it a syntax problem or what? I don't want to have to instantiate objects because for just calling utility functions and I need a few session variables to be stored globally. I do not want to call a init() function to store the global session variables every time I run a function either. Solutions?

+1  A: 

From the PHP manual:-

Like any other PHP static variable, static properties may only be initialized using a literal or constant; expressions are not allowed. So while you may initialize a static property to an integer or array (for instance), you may not initialize it to another variable, to a function return value, or to an object.

You say that you need your session variables to be stored globally? They are. $_SESSION is what is known as a "super global"

<?php

class utilities {
public static $color = $_SESSION['color']; //see here

 function display()   
 {      
     echo $_SESSION['color'];  
 }
}

utilities::display(); ?>
Mez
ahh shoot, what am I thinking I don't need to store them anywhere in the class because I can access them anywhere inside at any point without doing that. Silly mistake... To much coding is making me overthink simple problems I suppose. Thanks for bringing that to my attention.
payling
+2  A: 

In a class you can use SESSION only in methods...

Actually, if you wanna do something in a class, you must code it in a mehtod...

A class is not a function. It has some variables -as attributes- and some functions -as method- You can define variables, you can initilaize them. But you can't do any operation on them outside of a mehtod... for example

public static $var1;// OK!
public static $var2=5;//OK!
public static $var3=5+5;//ERROR

if you want to set them like this you must use constructor... (But remember; constructors doesn't called until the object created...)

<?php 
session_start();

$_SESSION['color'] = 'blue'; 

class utilities  {  

    public static $color;

    function __construct()
    {
        $this->color=$_SESSION['color'];
    }

    function display()     
    {          
        echo utilities::$color;  
    }
}
utilities::display(); //empty output, because; constructor didn't invoked...
$obj=new utilities();
echo "<br>".$obj->color;
?>
H2O