Object classes

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

    Object classes

    Hi

    I need to know how to keep instance of class. For example. I created
    object:

    $myObj = new Person();

    ok, and then someone wants to go to the links page, and I want to keep
    data which are created in myObj, for exmaple $myObj->age; $myObj->name;
    etc. etc.

    I thought about $serMyObj = serialize($myOb j) and the put $serMyObj
    data through POST, GET, or sth like that, and then GET it, but what if
    the portal is big and i want to keep 50 objects in one session ?? put
    data to the database and after that get it and every time someone is
    redirecting to the another page, get form database ...

  • Erwin Moller

    #2
    Re: Object classes

    Python wrote:
    [color=blue]
    > Hi
    >
    > I need to know how to keep instance of class. For example. I created
    > object:
    >
    > $myObj = new Person();
    >
    > ok, and then someone wants to go to the links page, and I want to keep
    > data which are created in myObj, for exmaple $myObj->age; $myObj->name;
    > etc. etc.
    >
    > I thought about $serMyObj = serialize($myOb j) and the put $serMyObj
    > data through POST, GET, or sth like that, and then GET it, but what if
    > the portal is big and i want to keep 50 objects in one session ?? put
    > data to the database and after that get it and every time someone is
    > redirecting to the another page, get form database ...[/color]

    Hi Python,

    I may make more sense for you to use SESSION instead of passing info around
    with POST and GET.
    Read here:


    Be sure you read the about Objects and Session, because there are some
    issues if you first start a session, and then declare the object.
    First load the objectdefinitio n, then start the session, but that is all
    explained at the php site.

    Good luck.

    Regards,
    Erwin Moller

    Comment

    • Justin Koivisto

      #3
      Re: Object classes

      Python wrote:[color=blue]
      > Hi
      >
      > I need to know how to keep instance of class. For example. I created
      > object:
      >
      > $myObj = new Person();
      >
      > ok, and then someone wants to go to the links page, and I want to keep
      > data which are created in myObj, for exmaple $myObj->age; $myObj->name;
      > etc. etc.
      >
      > I thought about $serMyObj = serialize($myOb j) and the put $serMyObj
      > data through POST, GET, or sth like that, and then GET it, but what if
      > the portal is big and i want to keep 50 objects in one session ?? put
      > data to the database and after that get it and every time someone is
      > redirecting to the another page, get form database ...
      >[/color]

      I think you answered your own question with this post... Use sessions.
      You can serialize your object and store it in a session variable. Then
      on the next script, make sure you include all the necessary include
      files for the object classes before you unserialize. I'd personally use
      sessions with custom handlers to utilize database tables for this, but
      sometimes that is overkill and takes more resources.

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

      Comment

      • Andy Jeffries

        #4
        Re: Object classes

        Justin Koivisto wrote:[color=blue]
        > I think you answered your own question with this post... Use sessions.
        > You can serialize your object and store it in a session variable. Then
        > on the next script, make sure you include all the necessary include
        > files for the object classes before you unserialize. I'd personally use
        > sessions with custom handlers to utilize database tables for this, but
        > sometimes that is overkill and takes more resources.[/color]

        Just for reference (at least with PHP 4.3+) you don't need to
        explicitely serialize/unserialize your objects for use in Sessions. You
        can just do:

        require_once("f oo.class.php");
        session_start() ;
        if (!is_object($_S ESSION["myfoo"])) {
        $_SESSION["myfoo"] = new Foo();
        }

        $_SESSION["myfoo"]->bar();

        Cheers,


        Andy

        Comment

        • Justin Koivisto

          #5
          Re: Object classes

          Andy Jeffries wrote:[color=blue]
          > Justin Koivisto wrote:
          >[color=green]
          >> I think you answered your own question with this post... Use sessions.
          >> You can serialize your object and store it in a session variable. Then
          >> on the next script, make sure you include all the necessary include
          >> files for the object classes before you unserialize. I'd personally
          >> use sessions with custom handlers to utilize database tables for this,
          >> but sometimes that is overkill and takes more resources.[/color]
          >
          >
          > Just for reference (at least with PHP 4.3+) you don't need to
          > explicitely serialize/unserialize your objects for use in Sessions. You
          > can just do:
          >
          > require_once("f oo.class.php");
          > session_start() ;
          > if (!is_object($_S ESSION["myfoo"])) {
          > $_SESSION["myfoo"] = new Foo();
          > }
          >
          > $_SESSION["myfoo"]->bar();[/color]

          You would not believe the number of ISPs still running the 4.0 and 4.1
          branches... However, it is good to point that out for those who have
          access to newer versions. I had that in my message, but deleted it as
          not to confuse the point.

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

          Comment

          • Andy Jeffries

            #6
            Re: Object classes

            Justin Koivisto wrote:[color=blue][color=green]
            >> Just for reference (at least with PHP 4.3+) you don't need to
            >> explicitely serialize/unserialize your objects for use in Sessions.
            >> You can just do:
            >>
            >> require_once("f oo.class.php");
            >> session_start() ;
            >> if (!is_object($_S ESSION["myfoo"])) {
            >> $_SESSION["myfoo"] = new Foo();
            >> }
            >>
            >> $_SESSION["myfoo"]->bar();[/color]
            >
            >
            > You would not believe the number of ISPs still running the 4.0 and 4.1
            > branches... However, it is good to point that out for those who have
            > access to newer versions. I had that in my message, but deleted it as
            > not to confuse the point.[/color]

            I'm not sure if it actually worked in older versions, it's been a while
            since I converted all my servers to 4.3...

            I haven't got any older versions to try it on. Have you tried it on an
            older version?

            Cheers,


            Andy

            Comment

            • Justin Koivisto

              #7
              Re: Object classes

              Andy Jeffries wrote:[color=blue]
              > Justin Koivisto wrote:
              >[color=green][color=darkred]
              >>> Just for reference (at least with PHP 4.3+) you don't need to
              >>> explicitely serialize/unserialize your objects for use in Sessions.
              >>> You can just do:
              >>>
              >>> require_once("f oo.class.php");
              >>> session_start() ;
              >>> if (!is_object($_S ESSION["myfoo"])) {
              >>> $_SESSION["myfoo"] = new Foo();
              >>> }
              >>>
              >>> $_SESSION["myfoo"]->bar();[/color]
              >>
              >>
              >>
              >> You would not believe the number of ISPs still running the 4.0 and 4.1
              >> branches... However, it is good to point that out for those who have
              >> access to newer versions. I had that in my message, but deleted it as
              >> not to confuse the point.[/color]
              >
              >
              > I'm not sure if it actually worked in older versions, it's been a while
              > since I converted all my servers to 4.3...
              >
              > I haven't got any older versions to try it on. Have you tried it on an
              > older version?[/color]

              I had done it once before and did have problems with objects in
              sessions, but I don't remember what version of PHP it was - of course,
              it could have been that server's setup as well. I've been making a point
              of not developing for versions under 4.3 as of late, and soon will be
              trying to push php5 much more. (When I've had some more time to play.)

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

              Comment

              Working...