How to work with a class in PHP?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    how to How to work with a class in PHP?

    I am back at php after a motorbike accident, and I have lost most of my memory, but not my love for web.
    Now I am just learning/exercising again, and I am facing first problems I can't handle/solve!

    In the php code I am writing, here is the situation:
    * there are six variables to deal with: $test1, $test2, $test3, $test4, $test5, $test6
    * 3 is passing through the function show() as a parameter (OK)
    * 4 is being called from within the function show() as a global (OK)
    * 1, 2, 5, 6 are not being shown. why?

    Thanks in advance!

    PHP Code:
    <?php
    
        class testClass {
            
            var $test1;
    
            public function __construct($test2) {
                $test2 = $test2;
                $test6 = 6;
            }
            
            public function show($test3) {
                global $test4;
                return $test1.$test2.$test3.$test4.$test5.$test6;
            }
    
        }
        
        $testClass = new testClass(2);
        $testClass->test1 = 1;
        $test4 = 4;
        $test5 = 5;
    
        echo $testClass->show(3);
    
    ?>
    mysterio.al - programming is a functional art

    #2
    use $this->test1.$this->test2 ......
    Advertise your mobile site for FREE with AdTwirl

    Comment


      #3
      Originally posted by GumSlone View Post
      use $this-&gt;test1.$this-&gt;test2 ......
      Good, thank you!
      What about $test5? Just make it GLOBAL as $test4?!
      mysterio.al - programming is a functional art

      Comment


        #4
        PHP Code:
        <?php
        
        class testClass {
        
        var $test1;
        
        public function __construct($test2) {
        $this->test2 = $test2;
        $this->test6 = 6;
        }
        
        public function show($test3) {
        global $test4, $test5;
        return $this->test1.$this->test2.$test3.$test4.$test5.$this->test6;
        }
        
        }
        
        $testClass = new testClass(2);
        $testClass->test1 = 1;
        $test4 = 4;
        $test5 = 5;
        
        echo $testClass->show(3);
        
        ?>
        Advertise your mobile site for FREE with AdTwirl

        Comment


          #5
          Originally posted by GumSlone View Post
          PHP Code:
          <?php
          
          class testClass {
          
          var $test1;
          
          public function __construct($test2) {
          $this->test2 = $test2;
          $this->test6 = 6;
          }
          
          public function show($test3) {
          global $test4, $test5;
          return $this->test1.$this->test2.$test3.$test4.$test5.$this->test6;
          }
          
          }
          
          $testClass = new testClass(2);
          $testClass->test1 = 1;
          $test4 = 4;
          $test5 = 5;
          
          echo $testClass->show(3);
          
          ?>
          Good, thank you! ;)
          mysterio.al - programming is a functional art

          Comment

          Working...
          X