1

After 9 hours of struggling to get this right, I have turned to the internet for help. I can't seem to find any relevant answers doing a Google search.

I currently have a class called Test. Test accepts a single argument.

<?php
    class test {
        private $varpassed;

        public function getVarpas() {
            return $this->varpassed;
        }

        Public function setVarpas($value) {
            $this->varpassed= $value;
        }

        public function stringGen(){

            $testvar = $this->varpassed;
            echo $testvar;
        }
    }

The stringGen function should return the $varpassed variable whenever its called. The value for $varpassed is set using the setVarpas function. However, when ever I call the stringGen() method I only seem to be getting the following error:

Fatal error: Using $this when not in object context in file.php line 14.

Pointing to this line:

$testvar = $this->varpassed;

Is there any other way to pass the variable to the stringGen method? I've tried using:

self::$this->varpassed;

Which also throws an error.

2
  • class test instead of class test() Commented Oct 3, 2015 at 20:08
  • Parse error: syntax error, unexpected '(', expecting '{' in F:\xampp\htdocs\testing\experiments\debug.php on line 2 rename it to class test instead Commented Oct 3, 2015 at 20:09

4 Answers 4

3

first create an instance of the object (so you can use $this in the context), for example:

$test = new test();

then you can call:

$test->setVarpas('Hello World!');

now you can call:

$test->stringGen();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. That worked. the way I was calling the function is like this: test::stringGen(); which was causing the issues. When i switched it to $test->stringGen(); it started working. Thanks again.
@DairyB Nice to hear that I could help you :) If you still want to use test::stringGen() you should make the property and methods static. This way you can use self:: as well. Have a nice day.
2

you have to do something like this

$var = new test();
$var->setVarpas("Hello");
$var->stringGen(); // this will echo Hello

$this is used when you are withing class. outside class you have to use class object.

2 Comments

I'm trying to get the variable passed to the method called stringGen, and then I want to return the variable viaa return function whenever the method is called. Creating the object and calling the method is no problem, but it doesn't seem to accept the variable in the method.
Thanks, used this as part of the solution. I'm unable to up vote, but thanks!
2

1) Change this: class test() to class test

2) Create and instance first something like $t1 = new test();

3) Call the function $t1->setVarpas(5);

4) Now you can call the function $t1->stringGen();

Fixed:

<?php
class test
{
private $varpassed;
public function getVarpas() {
return $this->varpassed;
}

Public function setVarpas($value) {
$this->varpassed= $value;
}

public function stringGen(){

$testvar = $this->varpassed;
echo $testvar;
}
}

$t1 = new test();
$t1->setVarpas(5);
$t1->stringGen();

OUTPUT:

5

2 Comments

$varpassed is not a static. the variable is dynamically assigned using the setVarpas method. Fixed the class though. Thanks.
@DairyB You're welcome! Stay safe and keep coding! ^_^
1

You should not declare a class with parentheses.

Use class test { instead of class test(){

1 Comment

Thanks, fixed on the application. still unable to pass the variable though.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.