Problem with classes

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

    Problem with classes


    Hello there!
    At the beginning, I'm apologize
    for my weak english skills.
    I got a problem with the class
    which I include from the other
    file. It's hard to me to explain
    it clearly, so I just give you
    files:

    index.php:

    <?php
    include("somecl ass.inc.php");
    $c=new classA();
    $c->SayHallo();
    include("anothe r.php");
    ?>

    comeclass.inc.p hp:

    <?php
    class classA()
    {
    function __construct()
    {
    echo 'Constructing.. .';
    }
    function SayHello()
    {
    echo "Hello, cruel world.";
    }
    }
    ?>

    another.php:

    <?php

    First try:
    $c->SayHello(); // error :/

    Second try:
    $c=new classA(); // ALMOST works. It
    $c->SayHallo(); // defines second
    // instance of the
    // class ClassA.
    // Unfortunetly, I
    // need to work on
    // the same instance.
    ?>

    Hope somebody helps me - how to define
    class and work with one and the same
    instance?

    --
    best regards
    Jag


  • Jerry Stuckle

    #2
    Re: Problem with classes

    Jag wrote:
    Hello there!
    At the beginning, I'm apologize
    for my weak english skills.
    I got a problem with the class
    which I include from the other
    file. It's hard to me to explain
    it clearly, so I just give you
    files:
    >
    index.php:
    >
    <?php
    include("somecl ass.inc.php");
    $c=new classA();
    $c->SayHallo();
    include("anothe r.php");
    ?>
    >
    comeclass.inc.p hp:
    >
    <?php
    class classA()
    {
    function __construct()
    {
    echo 'Constructing.. .';
    }
    function SayHello()
    {
    echo "Hello, cruel world.";
    }
    }
    ?>
    >
    another.php:
    >
    <?php
    >
    First try:
    $c->SayHello(); // error :/
    >
    Second try:
    $c=new classA(); // ALMOST works. It
    $c->SayHallo(); // defines second
    // instance of the
    // class ClassA.
    // Unfortunetly, I
    // need to work on
    // the same instance.
    ?>
    >
    Hope somebody helps me - how to define
    class and work with one and the same
    instance?
    >
    If this is another page (not included in either of the two previous
    files), then $c is no longer available. At the end of the page it gets
    destroyed.

    If you must work on the same instance, then you will need to store the
    object in the $_SESSION in the first page and retrieve in the second.


    And P.S. You're English skills are very good.

    --
    =============== ===
    Remove the "x" from my email address
    Jerry Stuckle
    JDS Computer Training Corp.
    jstucklex@attgl obal.net
    =============== ===

    Comment

    • ZeldorBlat

      #3
      Re: Problem with classes


      Jag wrote:
      Hello there!
      At the beginning, I'm apologize
      for my weak english skills.
      I got a problem with the class
      which I include from the other
      file. It's hard to me to explain
      it clearly, so I just give you
      files:
      >
      index.php:
      >
      <?php
      include("somecl ass.inc.php");
      $c=new classA();
      $c->SayHallo();
      include("anothe r.php");
      ?>
      >
      comeclass.inc.p hp:
      >
      <?php
      class classA()
      {
      function __construct()
      {
      echo 'Constructing.. .';
      }
      function SayHello()
      {
      echo "Hello, cruel world.";
      }
      }
      ?>
      >
      another.php:
      >
      <?php
      >
      First try:
      $c->SayHello(); // error :/
      >
      Second try:
      $c=new classA(); // ALMOST works. It
      $c->SayHallo(); // defines second
      // instance of the
      // class ClassA.
      // Unfortunetly, I
      // need to work on
      // the same instance.
      ?>
      >
      Hope somebody helps me - how to define
      class and work with one and the same
      instance?
      >
      --
      best regards
      Jag
      If you are, in fact, including another.php inside index.php where you
      say you are, then that shouldn't be a problem. What error are you
      getting? Could it be that the method of the class is called "SayHello"
      and you're trying to call "SayHallo" ?

      Comment

      • lorento

        #4
        Re: Problem with classes

        What version of PHP do you use?

        Old version of PHP doesn't support instantiate like this:
        $c=new classA();

        try to use below instantiate instead of above:
        $c = & new classA();

        --



        Jag wrote:
        Hello there!
        At the beginning, I'm apologize
        for my weak english skills.
        I got a problem with the class
        which I include from the other
        file. It's hard to me to explain
        it clearly, so I just give you
        files:

        Comment

        • Jerry Stuckle

          #5
          Re: Problem with classes

          lorento wrote:
          Jag wrote:
          >
          >>Hello there!
          >>At the beginning, I'm apologize
          >>for my weak english skills.
          >>I got a problem with the class
          >>which I include from the other
          >>file. It's hard to me to explain
          >>it clearly, so I just give you
          >>files:
          >
          >
          What version of PHP do you use?
          >
          Old version of PHP doesn't support instantiate like this:
          $c=new classA();
          >
          try to use below instantiate instead of above:
          $c = & new classA();
          >
          --


          >
          (Top posting fixed)

          Exactly which version doesn't support it? Every version of PHP I know
          of supports

          $c = new classA();

          P.S. Please don't top post.

          --
          =============== ===
          Remove the "x" from my email address
          Jerry Stuckle
          JDS Computer Training Corp.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          • Jag

            #6
            Re: Problem with classes

            User "Jerry Stuckle" <jstucklex@attg lobal.netwrote:
            [...]
            If you must work on the same instance, then you will
            need to store the object in the $_SESSION in the first
            page and retrieve in the second.
            It was so simple... I feel ashamed that I ask
            a question at all! :)
            Supposedly, the most simple things are
            most difficult. Enough for me.
            Thank you very much!
            And P.S. You're English skills are very good.
            It's nice to hear. But I know what I know :)
            Thanks again.

            --
            best regards
            Jag


            Comment

            • Colin Fine

              #7
              Re: Problem with classes

              Jerry Stuckle wrote:
              Jag wrote:
              >Hello there!
              >At the beginning, I'm apologize
              >for my weak english skills.
              >I got a problem with the class
              >which I include from the other
              >file. It's hard to me to explain
              >it clearly, so I just give you
              >files:
              >>
              >index.php:
              >>
              ><?php
              > include("somecl ass.inc.php");
              > $c=new classA();
              > $c->SayHallo();
              > include("anothe r.php");
              >?>
              >>
              >comeclass.inc. php:
              >>
              ><?php
              > class classA()
              > {
              > function __construct()
              > {
              > echo 'Constructing.. .';
              > }
              > function SayHello()
              > {
              > echo "Hello, cruel world.";
              > }
              > }
              >?>
              >>
              >another.php:
              >>
              ><?php
              >>
              > First try:
              > $c->SayHello(); // error :/
              >>
              > Second try:
              > $c=new classA(); // ALMOST works. It
              > $c->SayHallo(); // defines second
              > // instance of the
              > // class ClassA.
              > // Unfortunetly, I
              > // need to work on
              > // the same instance.
              >?>
              >>
              >Hope somebody helps me - how to define
              >class and work with one and the same
              >instance?
              >>
              >
              If this is another page (not included in either of the two previous
              files), then $c is no longer available. At the end of the page it gets
              destroyed.
              >
              It is another page, but it *is* included in index.php, after
              $c->SayHallo().

              As far as I can see from
              http://www.php.net/manual/en/languag...bles.scope.php, $c should be
              in scope inside another.php. I don't know why it is failing.
              If you must work on the same instance, then you will need to store the
              object in the $_SESSION in the first page and retrieve in the second.
              >
              This will presumably work, but I do not see why it should be necessary.
              >
              And P.S. You're English skills are very good.
              >
              Yes, better than you'res, Jerry! ;-)

              Colin

              Comment

              • Jerry Stuckle

                #8
                Re: Problem with classes

                Colin Fine wrote:
                Jerry Stuckle wrote:
                >>>
                >>
                >If this is another page (not included in either of the two previous
                >files), then $c is no longer available. At the end of the page it
                >gets destroyed.
                >>
                It is another page, but it *is* included in index.php, after
                $c->SayHallo().
                >
                Then it's not another page. It's just a different file included in the
                same page right? It should be available. But without all of the code
                involved, any guess would be just that - a guess. There are dozens of
                possibilities.
                As far as I can see from
                http://www.php.net/manual/en/languag...bles.scope.php, $c should be
                in scope inside another.php. I don't know why it is failing.
                >
                >If you must work on the same instance, then you will need to store the
                >object in the $_SESSION in the first page and retrieve in the second.
                >>
                This will presumably work, but I do not see why it should be necessary.
                >
                Well, if it's all included in the same page then you don't need to do it.
                >>
                >And P.S. You're English skills are very good.
                >>
                Yes, better than you'res, Jerry! ;-)
                >
                Colin

                --
                =============== ===
                Remove the "x" from my email address
                Jerry Stuckle
                JDS Computer Training Corp.
                jstucklex@attgl obal.net
                =============== ===

                Comment

                Working...