Jump to content

I don't understand these object class thingies


jimmyt1988

Recommended Posts

why is this not working?

 

    <?php
        class registration{                    
            public $email = $_POST["email"];
            public $username = $_POST["username"];
            public $password = $_POST["password"];
            //var $username = $_POST["confirmPassword"];
            function validateFields(){
                echo $this->$email;
                echo $this->$username;
                echo $this->$password;
                //echo $this->$confirmPassword;
            }
        }
        
        registration->validateFields();
    ?>

 

Im getting error: Parse error: syntax error, unexpected T_VARIABLE on the first property declaration $email.

It works with strings, so im guessing that parameters cant contain functions.

 

because $_POST is a function, i must declare each one as a public function.

 

agh, its nothing like javascript objects lol.

 

Any nudge in the right direction would be ACE.

 

EDIT:

 

Ok so i've ended up with this:

 

    <?php
        class registration{                    
            public function email(){echo $_POST["email"];}
            public function username(){echo $_POST["username"];}
            public function password(){echo $_POST["password"];}
            public function validateFields(){
                registration::email();
                registration::username();
                registration::password();
            }
        }
        
        registration::validateFields();
    ?>

 

Which works... But my problem is, I kinda wanted to batch these variables(parameters) into a class so i could call them from my various method i would have.

 

 

 

Ok im back. So i use Json all the time, i love it. very helpful, structured.

 

I need a like for like in terms of how to use it in my example.

 

I want to declare properties, username, password, email etc. then use them in a method.

 

Any examples u can push into the forum so i can learn from them?


    <?php
        class registration{                    
            public $fields = array("email", "username", "password");
            
            function validateFields(){            
                for ($i = 0; $i <= count($this->$fields); $i++){
                    echo $_POST[$this->$fields[$i]];
                }  
            }
        }
        
        registration::validateFields();
    ?>

 

Fatal error: Using $this when not in object context????

 

HUH??

hurrah,

 

I managed to get my variables echo'd on screen, alas i still have a few notices i dont quite understand. any help?

 


    <?php
        class registration{                    
            public $fields = array("email", "username", "password");
            
            function validateFields(){    
                for ($i = 0; $i <= count($this->fields); $i++){
                    echo $_POST[$this->fields[$i]];
                }  
            }
        }
        
        $obj = new registration();
        $obj->validateFields();
    ?>

 

Notice: Undefined offset: 3 in ** on line 11

 

Notice: Undefined index: in ** on line 11

 

how do i resolve these notices?

HU-BLOOMIN-RAY

 

YEHAA,

 

got it working. so stupid. so so stupid.

 



    <?php
        class registration{                    
            public $fields = array("email", "username", "password");
            
            function validateFields(){         
                for ($i = 0; $i < count($this->fields); $i++){
                    echo $_POST[$this->fields[$i]] . "<br />";
                }  
            }
        }
        
        $obj = new registration();
        $obj->validateFields();
    ?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.