Session/Class help

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Jon

    Session/Class help

    All,

    I'm currently building a custom Content Management system for a site we're
    working on, and am stuck. Currently, I am using a couple of classes to run
    most of the queries throughout the application. Well, I'm pretty stuck now.

    What I need to do is use a variable throughout my classes that is a Session
    variable. I really can't find another solution. The syntax I was using for
    this variable before (I actually hard coded it during my testing and
    development before this point) was this:

    class C_DisplayConten t{

    var $username = "S_";

    function foo(){
    //code that uses the variable as $this->username
    }//end foo

    }//end class

    So, what I really need now is to have the variable look like this:

    var $username = $_SESSION['username'];

    and be able to use it like normal using the $this->username syntax. However,
    PHP seems to blow up at it. I've tried numerous ways of doing this
    syntactically and I always end up with errors. The current error I'm getting
    is:

    Parse error: parse error, unexpected T_VARIABLE in

    The line is of course the line where I'm assigning the variable.

    What am I missing in regards to using session variables within a class? Any
    help is appreciated.


  • Norman Peelman

    #2
    Re: Session/Class help

    "Jon" <jonra@netins.c om> wrote in message
    news:du2k3c$enp $1@news.netins. net...[color=blue]
    > All,
    >
    > I'm currently building a custom Content Management system for a site we're
    > working on, and am stuck. Currently, I am using a couple of classes to run
    > most of the queries throughout the application. Well, I'm pretty stuck[/color]
    now.[color=blue]
    >
    > What I need to do is use a variable throughout my classes that is a[/color]
    Session[color=blue]
    > variable. I really can't find another solution. The syntax I was using for
    > this variable before (I actually hard coded it during my testing and
    > development before this point) was this:
    >
    > class C_DisplayConten t{
    >
    > var $username = "S_";
    >
    > function foo(){
    > //code that uses the variable as $this->username
    > }//end foo
    >
    > }//end class
    >
    > So, what I really need now is to have the variable look like this:
    >
    > var $username = $_SESSION['username'];
    >
    > and be able to use it like normal using the $this->username syntax.[/color]
    However,[color=blue]
    > PHP seems to blow up at it. I've tried numerous ways of doing this
    > syntactically and I always end up with errors. The current error I'm[/color]
    getting[color=blue]
    > is:
    >
    > Parse error: parse error, unexpected T_VARIABLE in
    >
    > The line is of course the line where I'm assigning the variable.
    >
    > What am I missing in regards to using session variables within a class?[/color]
    Any[color=blue]
    > help is appreciated.
    >
    >[/color]
    Since $_SESSION is a superglobal do you really need to assign it to
    $username?

    or at least try:

    var $username = '';

    function set_user_sessio n()
    {
    if (isset($_SESSIO N['username']) && !empty($_SESSIO N['username']))
    {
    $this->username = $_SESSION['username'];
    }
    else
    {
    $this->username = NULL; // or whatever you want
    }
    }

    Norm


    Comment

    • ZeldorBlat

      #3
      Re: Session/Class help


      Jon wrote:[color=blue]
      > All,
      >
      > I'm currently building a custom Content Management system for a site we're
      > working on, and am stuck. Currently, I am using a couple of classes to run
      > most of the queries throughout the application. Well, I'm pretty stuck now.
      >
      > What I need to do is use a variable throughout my classes that is a Session
      > variable. I really can't find another solution. The syntax I was using for
      > this variable before (I actually hard coded it during my testing and
      > development before this point) was this:
      >
      > class C_DisplayConten t{
      >
      > var $username = "S_";
      >
      > function foo(){
      > //code that uses the variable as $this->username
      > }//end foo
      >
      > }//end class
      >
      > So, what I really need now is to have the variable look like this:
      >
      > var $username = $_SESSION['username'];
      >
      > and be able to use it like normal using the $this->username syntax. However,
      > PHP seems to blow up at it. I've tried numerous ways of doing this
      > syntactically and I always end up with errors. The current error I'm getting
      > is:
      >
      > Parse error: parse error, unexpected T_VARIABLE in
      >
      > The line is of course the line where I'm assigning the variable.
      >
      > What am I missing in regards to using session variables within a class? Any
      > help is appreciated.[/color]

      You can't initialize a class member that way with anything other than a
      constant (for the most part). So function calls and other variables
      are out. You can, however, initialize it through the constructor:

      class foo {
      public $username;

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

      Note that, because $this->username is assigned by reference, changes to
      $this->username will also be reflected in $_SESSION (since they are two
      variables pointing to the same value).

      Comment

      • Jon

        #4
        Re: Session/Class help

        "Norman Peelman" <npeelman@cfl.r r.com> wrote in message
        news:uz5Nf.2116 6$_c.15839@torn ado.tampabay.rr .com...[color=blue]
        > "Jon" <jonra@netins.c om> wrote in message
        > news:du2k3c$enp $1@news.netins. net...[color=green]
        >> All,
        >>
        >> I'm currently building a custom Content Management system for a site
        >> we're
        >> working on, and am stuck. Currently, I am using a couple of classes to
        >> run
        >> most of the queries throughout the application. Well, I'm pretty stuck[/color]
        > now.[color=green]
        >>
        >> What I need to do is use a variable throughout my classes that is a[/color]
        > Session[color=green]
        >> variable. I really can't find another solution. The syntax I was using
        >> for
        >> this variable before (I actually hard coded it during my testing and
        >> development before this point) was this:
        >>
        >> class C_DisplayConten t{
        >>
        >> var $username = "S_";
        >>
        >> function foo(){
        >> //code that uses the variable as $this->username
        >> }//end foo
        >>
        >> }//end class
        >>
        >> So, what I really need now is to have the variable look like this:
        >>
        >> var $username = $_SESSION['username'];
        >>
        >> and be able to use it like normal using the $this->username syntax.[/color]
        > However,[color=green]
        >> PHP seems to blow up at it. I've tried numerous ways of doing this
        >> syntactically and I always end up with errors. The current error I'm[/color]
        > getting[color=green]
        >> is:
        >>
        >> Parse error: parse error, unexpected T_VARIABLE in
        >>
        >> The line is of course the line where I'm assigning the variable.
        >>
        >> What am I missing in regards to using session variables within a class?[/color]
        > Any[color=green]
        >> help is appreciated.
        >>
        >>[/color]
        > Since $_SESSION is a superglobal do you really need to assign it to
        > $username?
        >
        > or at least try:
        >
        > var $username = '';
        >
        > function set_user_sessio n()
        > {
        > if (isset($_SESSIO N['username']) && !empty($_SESSIO N['username']))
        > {
        > $this->username = $_SESSION['username'];
        > }
        > else
        > {
        > $this->username = NULL; // or whatever you want
        > }
        > }
        >
        > Norm
        >
        >[/color]

        Yup - that did it. I actually just killed the username var off and used
        $_SESSION['username'] throughout my application. I guess I didn't really
        think about it being global already... I'm new to PHP classes, so it's good
        to get this knowledge in. Thanks a ton for the help guys :)


        Comment

        Working...