can i have an array as a class variable?

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

    can i have an array as a class variable?

    hey there, is this ok?

    class MyClass
    {
    var $start;
    var $finish;
    function MyClass($start, $finish)
    {
    $this->start = $start;
    $this->finish=$finish ,
    $this->sensor_array = get_sensor_arra y();
    }
    function get_sensor_arra y
    {
    so some stuff to populate $this->sensor_array ;
    return array($this->sensor_array );
    }
    }

    another question i have,
    why do i need to declare variables with the var if i can declare them
    in the constructor without it ?

    just a couple of questions.
    if you have read this far, thanks for your time

    sk

  • Slant

    #2
    Re: can i have an array as a class variable?

    Hey nephish,

    You have some very good questions. I recently had some of these as
    well.

    class MyClass
    {
    var $start;
    var $finish;
    You don't HAVE to declair these variables here. It's just good
    practice to do so as you can change the scope of the variables here.
    function MyClass($start, $finish)
    If you're using PHP 5, I'd HIGHLY recommend that you use the following
    instead of what you are using here:

    function __construct($st art,$finish)

    Why? Because the method of building a construct using the same name as
    the class itself it old-school PHP 4 stuff. If you're still using PHP
    4, then by all means, keep at it. The "__construc t" method is the new
    method for PHP 5. Just cleans things up a bit! Give it a try. :)
    {
    $this->start = $start;
    $this->finish=$finish ,
    $this->sensor_array = get_sensor_arra y();
    }
    I see no reason why this should not work. In a recent post here in
    this same group, I posted a very similar example for a Database
    instantiation class:

    function __construct() {

    $db['host'] = "localhost" ;
    $db['user'] = "root";
    $db['pass'] = "";
    $db['name'] = "pbtportal" ;

    $link = mysql_connect($ db['host'],$db['user'],$db['pass']);
    mysql_select_db ($db['name'],$link);

    }

    Works like a charm! In this case, I did not declair the variable "$db"
    but probably should have.
    function get_sensor_arra y
    {
    so some stuff to populate $this->sensor_array ;
    return array($this->sensor_array );
    }
    }
    I hope that helps!

    Comment

    • nephish

      #3
      Re: can i have an array as a class variable?


      Slant wrote:
      Hey nephish,
      >
      You have some very good questions. I recently had some of these as
      well.
      >
      >
      class MyClass
      {
      var $start;
      var $finish;
      >
      You don't HAVE to declair these variables here. It's just good
      practice to do so as you can change the scope of the variables here.
      >
      function MyClass($start, $finish)
      >
      If you're using PHP 5, I'd HIGHLY recommend that you use the following
      instead of what you are using here:
      >
      function __construct($st art,$finish)
      >
      Why? Because the method of building a construct using the same name as
      the class itself it old-school PHP 4 stuff. If you're still using PHP
      4, then by all means, keep at it. The "__construc t" method is the new
      method for PHP 5. Just cleans things up a bit! Give it a try. :)
      >
      {
      $this->start = $start;
      $this->finish=$finish ,
      $this->sensor_array = get_sensor_arra y();
      }
      >
      I see no reason why this should not work. In a recent post here in
      this same group, I posted a very similar example for a Database
      instantiation class:
      >
      function __construct() {
      >
      $db['host'] = "localhost" ;
      $db['user'] = "root";
      $db['pass'] = "";
      $db['name'] = "pbtportal" ;
      >
      $link = mysql_connect($ db['host'],$db['user'],$db['pass']);
      mysql_select_db ($db['name'],$link);
      >
      }
      >
      Works like a charm! In this case, I did not declair the variable "$db"
      but probably should have.
      >
      function get_sensor_arra y
      {
      so some stuff to populate $this->sensor_array ;
      return array($this->sensor_array );
      }
      }
      >
      I hope that helps!
      Yes, this helps!
      thanks very much for your reply.
      i am testing code on a system with php5 and the production machine is
      php4. I know this is not the best idea, but it's what i have.
      i am using something similar to your code to have my user name and
      password out of the web folder. It just hasn't been in a class. cool.
      thanks again.
      sk

      Comment

      • Kimmo Laine

        #4
        Re: can i have an array as a class variable?

        "Slant" <rcross@gmail.c omwrote in message
        news:1157476551 .134715.190600@ d34g2000cwd.goo glegroups.com.. .
        I see no reason why this should not work. In a recent post here in
        this same group, I posted a very similar example for a Database
        instantiation class:
        >
        function __construct() {
        >
        $db['host'] = "localhost" ;
        $db['user'] = "root";
        $db['pass'] = "";
        $db['name'] = "pbtportal" ;
        >
        $link = mysql_connect($ db['host'],$db['user'],$db['pass']);
        mysql_select_db ($db['name'],$link);
        >
        }
        >
        Works like a charm! In this case, I did not declair the variable "$db"
        but probably should have.
        IMHO, those aren't class members, the $db elements. You should be using the
        $this pointer: $this->db[...]. Now they're only visible inside the scope of
        the __construct method. This is't exactly the same thing that nephish was
        asking...

        Still, the way nephish was doing it works fine, your example just doesn't
        demonstrate it very well.

        --
        "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
        http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
        spam@outolempi. net || Gedoon-S @ IRCnet || rot13(xvzzb@bhg byrzcv.arg)


        Comment

        • nephish

          #5
          Re: can i have an array as a class variable?


          Kimmo Laine wrote:
          "Slant" <rcross@gmail.c omwrote in message
          news:1157476551 .134715.190600@ d34g2000cwd.goo glegroups.com.. .
          I see no reason why this should not work. In a recent post here in
          this same group, I posted a very similar example for a Database
          instantiation class:

          function __construct() {

          $db['host'] = "localhost" ;
          $db['user'] = "root";
          $db['pass'] = "";
          $db['name'] = "pbtportal" ;

          $link = mysql_connect($ db['host'],$db['user'],$db['pass']);
          mysql_select_db ($db['name'],$link);

          }

          Works like a charm! In this case, I did not declair the variable "$db"
          but probably should have.
          >
          IMHO, those aren't class members, the $db elements. You should be using the
          $this pointer: $this->db[...]. Now they're only visible inside the scope of
          the __construct method. This is't exactly the same thing that nephish was
          asking...
          >
          Still, the way nephish was doing it works fine, your example just doesn't
          demonstrate it very well.
          >
          --
          "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
          http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
          spam@outolempi. net || Gedoon-S @ IRCnet || rot13(xvzzb@bhg byrzcv.arg)
          OK, i have a new question now.
          can i set a class object in a session variable ?

          like this:

          class Person
          {
          function Person($name)
          {
          $this->name = $name;
          }
          }

          $frank = new Person;
          $_SESSION['name'] = $frank;

          or is this just too much ?
          }

          Comment

          • Slant

            #6
            Re: can i have an array as a class variable?

            Fair enough. I did not mean to be unclear. I actually did not use the
            $this-specifier BECAUSE I did not want to be able to access this
            array anywhere but within the construct. Not defining a variable with
            $this-attached disallows other methods within the class from
            accessing it, does it not? It certainly does not work anyway. Maybe
            I'm missing something.

            To your question though, nephish. I don't see a problem with what you
            are doing. the Session variable, after all, is just another variable.
            You can store many things in variables, as you obviously know. What
            you're suggesting would work most efficiently if a single value was
            returned from the object that the session variable is being assigned
            to. For example:


            $frank = new Person;
            $_SESSION['auth'] = $frank->validate('fran k');

            class frank {
            function validate($name) {
            if ($name == auth (whatever method you choose to use for
            authorization)) {
            return true;
            } else {
            return false;
            }
            }
            }

            if ($_SESSION['auth'] == true) {
            echo "Yea! Fun to ensue!";
            }


            I'd probably not place the contents of the class directly into the
            Session variable, but instead call the object to do whatever you want,
            then return the data in an instance variable and assign THAT to the
            Session variable. How does that sound?


            $frank = new Person;
            $frank->validate('fran k');
            $_SESSION['auth'] = $frank->authorized;

            class frank {
            function validate($name) {
            if ($name == authorized (whatever method you choose to use for
            authorization)) {
            $this->authorized = true;
            } else {
            $this->authorized = false;
            }
            }
            }

            if ($_SESSION['auth'] == true) {
            echo "Yea! Fun to ensue!";
            }


            Kimmo Laine wrote:
            "Slant" <rcross@gmail.c omwrote in message
            news:1157476551 .134715.190600@ d34g2000cwd.goo glegroups.com.. .
            I see no reason why this should not work. In a recent post here in
            this same group, I posted a very similar example for a Database
            instantiation class:

            function __construct() {

            $db['host'] = "localhost" ;
            $db['user'] = "root";
            $db['pass'] = "";
            $db['name'] = "pbtportal" ;

            $link = mysql_connect($ db['host'],$db['user'],$db['pass']);
            mysql_select_db ($db['name'],$link);

            }

            Works like a charm! In this case, I did not declair the variable "$db"
            but probably should have.
            >
            IMHO, those aren't class members, the $db elements. You should be using the
            $this pointer: $this->db[...]. Now they're only visible inside the scope of
            the __construct method. This is't exactly the same thing that nephish was
            asking...
            >
            Still, the way nephish was doing it works fine, your example just doesn't
            demonstrate it very well.
            >
            --
            "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
            http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
            spam@outolempi. net || Gedoon-S @ IRCnet || rot13(xvzzb@bhg byrzcv.arg)

            Comment

            • nephish

              #7
              Re: can i have an array as a class variable?


              Slant wrote:
              Fair enough. I did not mean to be unclear. I actually did not use the
              $this-specifier BECAUSE I did not want to be able to access this
              array anywhere but within the construct. Not defining a variable with
              $this-attached disallows other methods within the class from
              accessing it, does it not? It certainly does not work anyway. Maybe
              I'm missing something.
              >
              To your question though, nephish. I don't see a problem with what you
              are doing. the Session variable, after all, is just another variable.
              You can store many things in variables, as you obviously know. What
              you're suggesting would work most efficiently if a single value was
              returned from the object that the session variable is being assigned
              to. For example:
              >
              >
              $frank = new Person;
              $_SESSION['auth'] = $frank->validate('fran k');
              >
              class frank {
              function validate($name) {
              if ($name == auth (whatever method you choose to use for
              authorization)) {
              return true;
              } else {
              return false;
              }
              }
              }
              >
              if ($_SESSION['auth'] == true) {
              echo "Yea! Fun to ensue!";
              }
              >
              >
              I'd probably not place the contents of the class directly into the
              Session variable, but instead call the object to do whatever you want,
              then return the data in an instance variable and assign THAT to the
              Session variable. How does that sound?
              >
              >
              $frank = new Person;
              $frank->validate('fran k');
              $_SESSION['auth'] = $frank->authorized;
              >
              class frank {
              function validate($name) {
              if ($name == authorized (whatever method you choose to use for
              authorization)) {
              $this->authorized = true;
              } else {
              $this->authorized = false;
              }
              }
              }
              >
              if ($_SESSION['auth'] == true) {
              echo "Yea! Fun to ensue!";
              }
              >
              >
              Kimmo Laine wrote:
              "Slant" <rcross@gmail.c omwrote in message
              news:1157476551 .134715.190600@ d34g2000cwd.goo glegroups.com.. .
              I see no reason why this should not work. In a recent post here in
              this same group, I posted a very similar example for a Database
              instantiation class:
              >
              function __construct() {
              >
              $db['host'] = "localhost" ;
              $db['user'] = "root";
              $db['pass'] = "";
              $db['name'] = "pbtportal" ;
              >
              $link = mysql_connect($ db['host'],$db['user'],$db['pass']);
              mysql_select_db ($db['name'],$link);
              >
              }
              >
              Works like a charm! In this case, I did not declair the variable "$db"
              but probably should have.
              IMHO, those aren't class members, the $db elements. You should be usingthe
              $this pointer: $this->db[...]. Now they're only visible inside the scope of
              the __construct method. This is't exactly the same thing that nephish was
              asking...

              Still, the way nephish was doing it works fine, your example just doesn't
              demonstrate it very well.

              --
              "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
              http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
              spam@outolempi. net || Gedoon-S @ IRCnet || rot13(xvzzb@bhg byrzcv.arg)
              ok, i get you, yeah, i think thats cleaner too.
              thanks for your time.
              nephish

              Comment

              • Kimmo Laine

                #8
                Re: can i have an array as a class variable?

                "nephish" <nephish@gmail. comwrote in message
                news:1157550802 .659286.20280@b 28g2000cwb.goog legroups.com...
                OK, i have a new question now.
                can i set a class object in a session variable ?
                >
                like this:
                >
                class Person
                {
                function Person($name)
                {
                $this->name = $name;
                }
                }
                >
                $frank = new Person;
                $_SESSION['name'] = $frank;
                >
                or is this just too much ?
                }
                Not just like that, you must first convert the object to a storeable format,
                it's called serializing. There is a function called serialize() and it's
                compliment function unserialize(). When you store the object to session, you
                serialize it first. then when you need to revert it back from the session,
                you unserialize it. And also, the class definition must be available on each
                page where the object is accessed. If it is missing, the object can't be
                unserialized.

                on page1.php:
                $_SESSION['name'] = serialize($fran k);

                on page2.php:
                $frank = unserialize($_S ESSION['name']);

                --
                "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
                http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
                spam@outolempi. net || Gedoon-S @ IRCnet || rot13(xvzzb@bhg byrzcv.arg)



                Comment

                • nephish

                  #9
                  Re: can i have an array as a class variable?


                  Kimmo Laine wrote:
                  "nephish" <nephish@gmail. comwrote in message
                  news:1157550802 .659286.20280@b 28g2000cwb.goog legroups.com...
                  OK, i have a new question now.
                  can i set a class object in a session variable ?

                  like this:

                  class Person
                  {
                  function Person($name)
                  {
                  $this->name = $name;
                  }
                  }

                  $frank = new Person;
                  $_SESSION['name'] = $frank;

                  or is this just too much ?
                  }
                  >
                  Not just like that, you must first convert the object to a storeable format,
                  it's called serializing. There is a function called serialize() and it's
                  compliment function unserialize(). When you store the object to session, you
                  serialize it first. then when you need to revert it back from the session,
                  you unserialize it. And also, the class definition must be available on each
                  page where the object is accessed. If it is missing, the object can't be
                  unserialized.
                  >
                  on page1.php:
                  $_SESSION['name'] = serialize($fran k);
                  >
                  on page2.php:
                  $frank = unserialize($_S ESSION['name']);
                  >
                  --
                  "Ohjelmoija on organismi joka muuttaa kofeiinia koodiksi" - lpk
                  http://outolempi.net/ahdistus/ - Satunnaisesti päivittyvä nettisarjis
                  spam@outolempi. net || Gedoon-S @ IRCnet || rot13(xvzzb@bhg byrzcv.arg)
                  Well, thanks ! this could make my sessions and keeping track of state a
                  whole lot easier. I had looked at serialize, but i thought that it had
                  to be set in a database. I didn't know it was that easy.

                  thanks a lot.

                  shawn

                  Comment

                  Working...