How to work with a class in PHP?

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

  • Mysterio
    replied
    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! ;)

    Leave a comment:


  • GumSlone
    replied
    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);
    
    ?>

    Leave a comment:


  • Mysterio
    replied
    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?!

    Leave a comment:


  • GumSlone
    replied
    use $this->test1.$this->test2 ......

    Leave a comment:


  • Mysterio
    started a topic how to How to work with a class in PHP?

    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);
    
    ?>
Working...
X