1

I'm practising coding in an object oriented way; experimenting with taking some frequently used scripts for form handling and turning them into functions. Here is my code.

    class FormHandler{

        // Secure simple inputs
        public function secure($var){
                $var = stripslashes($var);
                $var = strip_tags($var);
                $var = htmlentities($var);
                return $var;
        }

        public function getAll(){
                foreach($_POST as $key => $value){
                ${$key} = secure($_POST[$key]);
                }
                $didGetAll =TRUE;
        }

        public function echoResults(){
            if($didGetAll === TRUE){
                echo "Form Contents<br>";
                foreach($_POST as $key => $value){
                    echo $key." => ".${$key}."<br>";
                }
            }else{
                    echo 'do getAll() fuction first'."<br>";
                }
    }
    } 

When I run the functions like so:

include './formhandling.php';

$form = new FormHandler;
$form -> getAll();
$form -> echoResults();

it returns the 'do getAll() fuction first' message even though the $didGetAll var should = true.

I assume this is because the variable values aren't being passed between functions?

I've tried to test this by making $didGetAll global, and by doing return $didGetAll. But it still returns the same result.

Could someone suggest what I'm doing wrong?

1

2 Answers 2

2

Use $didGetAll as a property in your class to access in object.

class FormHandler{
    private $didGetAll = FALSE;
    // Secure simple inputs
    public function secure($var){
        $var = stripslashes($var);
        $var = strip_tags($var);
        $var = htmlentities($var);
        return $var;
    }

    public function getAll(){
        foreach($_POST as $key => $value){
            $this->$key = $this->secure($_POST[$key]);
        }
        $this->didGetAll =TRUE;
    }

    public function echoResults(){
        if($this->didGetAll === TRUE){
            echo "Form Contents<br>";
            foreach($_POST as $key => $value){
                echo $key." => ".$this->$key."<br>";
            }
        }else{
                echo 'do getAll() fuction first'."<br>";
            }
    }
} 
Sign up to request clarification or add additional context in comments.

6 Comments

Thanks. This works for $didGetAll but i notice now ${$key} echos no value presumably for the same reason. is there a similar way to do this with a dynamic variable name?
Yes, i updated my answer. If you use $this->$key = , create property in your object with $key as name. @JamieMoffat
If the answer worked, please accept it. @JamieMoffat
I'm afraid it it still only worked for the $didGetAll var. I still can't get it to work for the variable variable ${$key} I'll update my post to reflect this.
I updated my answer. If you use $this->$key = , create property in your object with $key as name and you can access with $this->key in object. @JamieMoffat
|
0

Use public property for this, it`s allow to store data in variables inside class:

class FormHandler{
    public $didGetAll = false;

    // Secure simple inputs
    public function secure($var){
            $var = stripslashes($var);
            $var = strip_tags($var);
            $var = htmlentities($var);
            return $var;
    }

    public function getAll(){
            foreach($_POST as $key => $value){
            ${$key} = secure($_POST[$key]);
            }
            $this->didGetAll =TRUE;
    }

    public function echoResults(){
        if($this->didGetAll === TRUE){
            echo "Form Contents<br>";
            foreach($_POST as $key => $value){
                echo $key." => ".${$key}."<br>";
            }
        }else{
            echo 'do getAll() fuction first'."<br>";
        }
    }
} 

$form = new FormHandler;
$form -> getAll();
$form -> echoResults();

Read this for understand it.

Comments

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.