storing classes

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • paladin.rithe@gmail.com

    storing classes

    Is it possible to store classes in an array? I am fairly new to PHP,
    and haven't found anything either way yet.
    I have a program that where you can have multiple notes attached to a
    ticket, which are stored in a database. I would like to just pull all
    the notes from the database, storing each one in a seperate class,
    which itself is stored in an array (well, another class, but it's a
    classList, it's mainly an array).
    I'm getting an error when I run the page:
    "Cannot use a scalar value as an array", and "Call to a member function
    on a non-object". The second is related to the first, as I'm calling a
    function on the array value.
    If it is possible, I probably coded something wrong. If that's the
    case, I'll post some code.
    Thanks for the help.

  • Carl Vondrick

    #2
    Re: storing classes

    You can "store" objects in an array, sure:

    $my_array = array(new my_object(), new_my_object() );

    That should work just fine (untested).

    You can also try and serialize objects, which are useful if you want to
    save them after execution is over. To do this, you use serialize()and
    unserialize(). For example:

    fwrite($handle, serialize($myob ject));

    Then later on:

    $a = unserialize(fil e_get_contents( 'serialized_cod e.php'));

    Good luck.

    Comment

    • Justin Koivisto

      #3
      Re: storing classes

      Carl Vondrick wrote:[color=blue]
      > You can "store" objects in an array, sure:
      >
      > $my_array = array(new my_object(), new_my_object() );
      >
      > That should work just fine (untested).
      >
      > You can also try and serialize objects, which are useful if you want to
      > save them after execution is over. To do this, you use serialize()and
      > unserialize(). For example:
      >
      > fwrite($handle, serialize($myob ject));
      >
      > Then later on:
      >
      > $a = unserialize(fil e_get_contents( 'serialized_cod e.php'));[/color]

      Be sure that you have included the class definitions *before* you
      attempt to unserialize an object, or you may have some problems.

      I haven't tried serializing objects in php, but I was just thinking that
      in php5 because of the pass by reference default nature, you may have
      difficulty with it if you aren't using the clone keyword...

      --
      Justin Koivisto, ZCE - justin@koivi.co m

      Comment

      • Iván Sánchez Ortega

        #4
        Re: storing classes

        -----BEGIN PGP SIGNED MESSAGE-----
        Hash: SHA1

        paladin.rithe@g mail.com wrote:
        [color=blue]
        > Is it possible to store classes in an array? I am fairly new to PHP,
        > and haven't found anything either way yet.[/color]

        Classes? No. Objects? Definively yes.
        [color=blue]
        > "Cannot use a scalar value as an array", and "Call to a member function
        > on a non-object". The second is related to the first, as I'm calling a
        > function on the array value.[/color]

        My guess is that you are doing something like:

        <?php

        $object = new foo;
        $array[$object] = $counter;

        ?>

        When you should be doing:

        <?php

        $object = new foo;
        $array[$counter] = $object;

        ?>

        - --
        - ----------------------------------
        Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

        http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
        MSN:i_eat_s_p_a _m_for_breakfas t@hotmail.com
        Jabber:ivansanc hez@jabber.org ; ivansanchez@kde talk.net
        -----BEGIN PGP SIGNATURE-----
        Version: GnuPG v1.4.2 (GNU/Linux)

        iD8DBQFD1pX33jc Q2mg3Pc8RAmsNAJ wKqNtfSPj0kZ6T0 eBFucdaywxohQCf Q/Wk
        SAaNzt8xfdIQCzO lG6zK4LA=
        =kW+r
        -----END PGP SIGNATURE-----

        Comment

        • paladin.rithe@gmail.com

          #5
          Re: storing classes

          I think I see the issue. What I'm doing is this:

          <?php

          $array[] = new foo;
          $array[$num]->foofunction( );

          ?>

          I'll bet it doesn't like that. I'll try doing it your way first to see
          what happens.

          Comment

          • Iván Sánchez Ortega

            #6
            Re: storing classes

            -----BEGIN PGP SIGNED MESSAGE-----
            Hash: SHA1

            paladin.rithe@g mail.com wrote:
            [color=blue]
            > $array[] = new foo;
            > $array[$num]->foofunction( );[/color]

            Are you sure that $array[$num] contains an instance of foo?? What is $num???

            - --
            - ----------------------------------
            Iván Sánchez Ortega -i-punto-sanchez--arroba-mirame-punto-net

            http://acm.asoc.fi.upm.es/~mr/ ; http://acm.asoc.fi.upm.es/~ivan/
            MSN:i_eat_s_p_a _m_for_breakfas t@hotmail.com
            Jabber:ivansanc hez@jabber.org ; ivansanchez@kde talk.net
            -----BEGIN PGP SIGNATURE-----
            Version: GnuPG v1.4.2 (GNU/Linux)

            iD8DBQFD1qcS3jc Q2mg3Pc8RAhZ+AJ wMUGKh+Q/nJI3zfHJ9qZJ4AJ UEdwCfSZld
            3fe0spkCWoDHOTF oTVqSQiM=
            =xlZG
            -----END PGP SIGNATURE-----

            Comment

            • Carl Vondrick

              #7
              Re: storing classes

              Justin Koivisto wrote:[color=blue]
              > I haven't tried serializing objects in php, but I was just thinking that
              > in php5 because of the pass by reference default nature, you may have
              > difficulty with it if you aren't using the clone keyword...[/color]


              I just tried it out on my machine: it works as expected.

              <?php
              class MyObject
              {
              public $name;

              public function __construct()
              {
              $this->name = 'Carl';
              }
              }

              $a = new MyObject;
              $b = & $a;

              print serialize($b) . "\n" . serialize($a);
              ?>

              Returns:
              O:8:"MyObject": 1:{s:4:"name";s :4:"Carl";}
              O:8:"MyObject": 1:{s:4:"name";s :4:"Carl";}

              Comment

              • paladin.rithe@gmail.com

                #8
                Re: storing classes

                I'm doing the work in a loop, and $num would be the location of the
                object: like $array[1]->foofunction( );
                I changed my code to the other way though, and it seems to be working
                correctly. Now it's just not pulling information, or pulling the wrong
                information, I'm not sure yet. But that's something else entirely.
                Thanks for the help everyone.

                Comment

                Working...