4

Is this just impossible?

I thought I would clean up some of my code and put db queries in models only where they belong and put all the other code that belongs in controllers in controllers.

Now I keep getting undefined variable errors. Which is not a problem but I'm trying to work out how to call variables between files.

I would simply like the random hash generated at registration .. stored in a variable because that's the variable I use in the anchor for the "click here to activate account" link that is sent to users email.

I also use that same variable in the method that compares the uri hash that's at the end of the URL in their email with the one stored in the database.. in order for user to confirm their account and update "status" in database to 1(activated).

I would really appreciate some advice. I'm enjoying this learning process. Loosing sleep but enjoying it as it make me think logically.

1 Answer 1

5

You cannot access a variable if it's in a seperate file, instead you should set it in your class.

class User_model extends Model {

    // Declare the foo variable
    public $foo = "";

    function blah() {

        // You can set variable foo this way from any controller/model that includes this model
        $this->foo = "dog";

        // You can access variable foo this way
        echo $this->foo;
    }
}
Sign up to request clarification or add additional context in comments.

3 Comments

Yes you can set a variable to the return of a function but that doesn't seem to be what you were asking
My problem is when I do my coding and I use a method say from a model in a controller I have trouble with undefined variables. Like the variable I set in the method in a model can't be used in a method in the controller that I load that model method from. That is one problem I run into and the other is sometimes when I pass variables from 1 method to another in a model or a controller.
Just a quick note that if you need to call the function blah() from your construct within a model you will no longer be able to echo $this->foo as you will get a "headers already sent" error. You DO still have access to the var value in your function blah() but if you specifically need to echo something it has to be done from the view not the model.

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.