How to work with a class in PHP?

Started on the old forum in May 2014 · view the original in the archive · old links redirect here

#class #oop #php

4 replies · 148 views · started 12 years ago · 1 min read

2 participants
Mysterio Lead · 630 posts #1

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

    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

Mysterio Lead · 630 posts #3

GumSlone wrote: use $this->test1.$this->test2 ......

Good, thank you! What about $test5? Just make it GLOBAL as $test4?!

mysterio.al - programming is a functional art

GumSlone Administrator Principal · 1,690 posts #4
<?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);

?>
👍 1
Share

Advertise your mobile site for FREE with AdTwirl

External image (may be gone)

Mysterio Lead · 630 posts #5

GumSlone wrote:

<?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

Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close