Class problem

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

    Class problem

    consider this:

    ?

    class A {

    function A() {
    $b = 'hello world';
    }

    function B() {
    $c = new C();
    $c->doStuff();
    }

    }

    class C {

    function C() {
    $blah = 0;
    }

    function doStuff() {
    echo 'foo bar ' . A::b;
    }

    }

    $a = new A();

    $a->B();

    ?>

    I get a parse error on line 23 with
    echo 'foo bar ' . A::b;

    But I have to retrieve the property $b from class A FROM the doStuff()
    method in Class C...

    how do I do that?

    Thanx
    Phil


  • Phil Powell

    #2
    Re: Class problem

    Sorry this is more accurate:

    <?

    class A {

    function A() {
    $graduation_mon th = '05';
    $c = new C();
    $c->doStuff();
    }

    }

    class C {

    function C() {
    $blah = 0;
    }

    function doStuff() {

    }

    }

    $a = new A();

    $a->A();

    ?>

    Except that doStuff() needs to get the name 'graduation_mon th' and the value
    $graduation_mon th that are in A. Not sure how.

    Phil


    Comment

    • Phil Powell

      #3
      Re: Class problem

      Ok I gave up on the class idea and decided on a simpler approach: include a
      PHP script with a function that will return an array with elements being the
      HTML strings I need. I however, have to have the showApplicant class
      because that's been pre-existing in the original site structure since I
      started working where I work at, so getting rid of it will pretty much
      destroy most of the existing site. So here is something that I would want:

      <?
      function DropdownGenerat or($monthName = '', $dayName = '', $yearName = '')
      {
      $blah = array("$monthNa me = ${$monthName}",
      "$dayName = ${$dayName}",
      "$yearName = ${$yearName}"
      );
      return $blah;
      }

      class showApplicant {

      function showApplicant() {
      $graduation_mon th = '05';
      $graduation_yea r = '2001';
      print_r(Dropdow nGenerator('gra duation_month', '', 'graduation_yea r'));
      }

      }

      $app = new showApplicant() ;


      ?>


      However, I cannot still get the values of $graduation_mon th, etc. I cannot
      change showApplicant class no matter what, but I can change the
      DropdownGenerat or function, how must I do so to make sense?

      Phil


      Comment

      • Bruno Desthuilliers

        #4
        Re: Class problem

        Phil Powell wrote:[color=blue]
        > Sorry this is more accurate:
        >
        > <?
        >
        > class A {
        >
        > function A() {
        > $graduation_mon th = '05';
        > $c = new C();
        > $c->doStuff();
        > }
        >
        > }
        >
        > class C {
        >
        > function C() {
        > $blah = 0;
        > }
        >
        > function doStuff() {
        >
        > }
        >
        > }
        >
        > $a = new A();
        >
        > $a->A();
        >
        > ?>
        >
        > Except that doStuff() needs to get the name 'graduation_mon th' and the value
        > $graduation_mon th that are in A. Not sure how.[/color]

        What do you think function args are for ?

        class C {
        function doStuff($name, $value) {
        // do whatever with $name and $value
        }
        }

        class A {
        function A() {
        $graduation_mon th = '05';
        $c = new C();
        $c->doStuff('gradu ation_month', $graduation_mon th);
        }
        }

        Bruno

        Comment

        • Bruno Desthuilliers

          #5
          Re: Class problem

          Phil Powell wrote:[color=blue]
          > consider this:
          >
          > ?
          >
          > class A {
          >
          > function A() {
          > $b = 'hello world';
          > }
          >
          > function B() {
          > $c = new C();
          > $c->doStuff();
          > }
          >
          > }
          >
          > class C {
          >
          > function C() {
          > $blah = 0;
          > }
          >
          > function doStuff() {
          > echo 'foo bar ' . A::b;[/color]

          b is not a property of class A, it's a local variable in method A::A().
          if you want b to be a property of class A, you need to declare class A
          like this :

          class A {
          var $b = 'hello world';

          // etc
          }

          Now by accessing b with the A::b syntax, you'll have the same value for
          all and every instances of A (the value you initialized A::b with).

          I think what you want is :

          class C {
          function doStuff($a) {
          echo 'foo bar ' . $a->b;
          }
          }

          class A {
          var b = '';

          function A() {
          $this->b = 'baaz';
          }

          function fun() {
          $c = new C();
          $c->doStuff($this) ;
          }
          }


          HTH
          Bruno

          Comment

          • Michael Rasmussen

            #6
            Re: Class problem

            Den Thu, 16 Oct 2003 01:18:45 -0400. skrev Phil Powell:
            [color=blue]
            >
            > However, I cannot still get the values of $graduation_mon th, etc. I
            > cannot change showApplicant class no matter what, but I can change the
            > DropdownGenerat or function, how must I do so to make sense?
            >[/color]
            Why not:
            class DropdownGenerat or {
            var $month = array(
            '0' => 'January',
            '1' => 'February',
            etc..
            );
            var $day = array(
            '0' => 'Monday',
            '1' => 'Tuesday',
            etc.
            );
            var $year = array(
            '0' => 'xxxx',
            '1' => 'yyyy',
            etc.
            );
            var $blah;

            function getDropdownGene rator($monthNam e, $dayName, $yearName) {
            $this->blah = array(
            $monthName => $month->$monthName,
            $dayName => $day->$dayName,
            $yearName => $year->$yearName
            );
            return $this->blah;
            }
            }

            class ShowApplicant extends DropdownGenerat or {

            var $graduation_mon th;
            var $graduation_yea r;
            var $graduation_day ;

            function showApplicant($ d = '', $m = '', $y = '') {
            $this->graduation_mon th = $m;
            $this->graduation_yea r = $y;
            $this->graduation_d ay = $d;
            }

            function setOptions($d, $m, $y) {
            $this->graduation_mon th = $m;
            $this->graduation_yea r = $y;
            $this->graduation_d ay = $d;
            }

            function getPrint() {
            return getDropdownGene rator(
            $this->graduation_mon th,
            $this->graduation_day ,
            $this->graduation_yea r
            );
            }
            }

            $obj = &new ShowApplicant() ;
            $obj->setOptions('1' ,'1','2003');
            print_r($obj->getPrint());

            Ontested!

            --
            Hilsen/Sincerely, Michael Rasmussen

            En windows admin er en person, for hvem den største bedrift er, at
            lave konfiguration af serveren med trial and error via en gui.

            Comment

            • Phil Powell

              #7
              Re: Class problem

              I'm sorry, I understood none of what you said. Je ne comprends pas
              tout ce que tu as ecrit a moi.

              In fact, I wound up with a solution on my own involving variable
              bundling and passing collections into the class instead.
              Phil


              Bruno Desthuilliers <bdesth.nospam@ removeme.free.f r> wrote in message news:<3f8e6448$ 0$27588$626a54c e@news.free.fr> ...[color=blue]
              > Phil Powell wrote:[color=green]
              > > consider this:
              > >
              > > ?
              > >
              > > class A {
              > >
              > > function A() {
              > > $b = 'hello world';
              > > }
              > >
              > > function B() {
              > > $c = new C();
              > > $c->doStuff();
              > > }[/color]
              >[color=green]
              > > }
              > >
              > > class C {
              > >
              > > function C() {
              > > $blah = 0;
              > > }
              > >
              > > function doStuff() {
              > > echo 'foo bar ' . A::b;[/color]
              >
              > b is not a property of class A, it's a local variable in method A::A().
              > if you want b to be a property of class A, you need to declare class A
              > like this :
              >
              > class A {
              > var $b = 'hello world';
              >
              > // etc
              > }
              >
              > Now by accessing b with the A::b syntax, you'll have the same value for
              > all and every instances of A (the value you initialized A::b with).
              >
              > I think what you want is :
              >
              > class C {
              > function doStuff($a) {
              > echo 'foo bar ' . $a->b;
              > }
              > }
              >
              > class A {
              > var b = '';
              >
              > function A() {
              > $this->b = 'baaz';
              > }
              >
              > function fun() {
              > $c = new C();
              > $c->doStuff($this) ;
              > }
              > }
              >
              >
              > HTH
              > Bruno[/color]

              Comment

              • Phil Powell

                #8
                Re: Class problem

                Sorry I can't do that. If I rewrote showApplicant like that nearly
                the entire prewritten site I inherited would fail because
                showApplicant has to be standalone.

                What I wound up doing was variable bundling into a new class I wrote,
                DateGroupHTMLGe nerator and handling it that way, works perfectly,
                except that there is now some bug involving array_search and
                array_keys (more on that in another post).
                Thanx though

                Phil

                Michael Rasmussen <mir@datanom.ne t> wrote in message news:<pan.2003. 10.16.10.48.53. 146570@datanom. net>...[color=blue]
                > Den Thu, 16 Oct 2003 01:18:45 -0400. skrev Phil Powell:
                >[color=green]
                > >
                > > However, I cannot still get the values of $graduation_mon th, etc. I
                > > cannot change showApplicant class no matter what, but I can change the
                > > DropdownGenerat or function, how must I do so to make sense?
                > >[/color]
                > Why not:
                > class DropdownGenerat or {
                > var $month = array(
                > '0' => 'January',
                > '1' => 'February',
                > etc..
                > );
                > var $day = array(
                > '0' => 'Monday',
                > '1' => 'Tuesday',
                > etc.
                > );
                > var $year = array(
                > '0' => 'xxxx',
                > '1' => 'yyyy',
                > etc.
                > );
                > var $blah;
                >
                > function getDropdownGene rator($monthNam e, $dayName, $yearName) {
                > $this->blah = array(
                > $monthName => $month->$monthName,
                > $dayName => $day->$dayName,
                > $yearName => $year->$yearName
                > );
                > return $this->blah;
                > }
                > }
                >
                > class ShowApplicant extends DropdownGenerat or {
                >
                > var $graduation_mon th;
                > var $graduation_yea r;
                > var $graduation_day ;
                >
                > function showApplicant($ d = '', $m = '', $y = '') {
                > $this->graduation_mon th = $m;
                > $this->graduation_yea r = $y;
                > $this->graduation_d ay = $d;
                > }
                >
                > function setOptions($d, $m, $y) {
                > $this->graduation_mon th = $m;
                > $this->graduation_yea r = $y;
                > $this->graduation_d ay = $d;
                > }
                >
                > function getPrint() {
                > return getDropdownGene rator(
                > $this->graduation_mon th,
                > $this->graduation_day ,
                > $this->graduation_yea r
                > );
                > }
                > }
                >
                > $obj = &new ShowApplicant() ;
                > $obj->setOptions('1' ,'1','2003');
                > print_r($obj->getPrint());
                >
                > Ontested![/color]

                Comment

                Working...