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.

Link to comment
Share on other sites

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.

 

 

 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites


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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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();
    ?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.