0

the class below is part of an mvc structure im building for my website. the class is there to produce a page if index.php is called without any request in the url. if that is good practice or not I do not know. Im new to php, and this is the first time i implement an mvc structure.

<?php

class Default_Model
{
    private $defaultPage = array
    (
        'headline' => 'JOBBSÖKAREN', 
        'instruction' => 'logga in nedanför'    
    );

    public function __construct()
    {
    }

    public function getContent()
    {
line 17     return $defaultPage;
    }
}
?>

here is the error message: Notice: Undefined variable: defaultPage in /home/mengus/dev/www/models/default.php on line 17

so what I dont get is why the array is undefined. I use similar code in other classes and it works just fine. is it a scope issue? I seem to have gone error blind from staring at this :) thanks for help.

1 Answer 1

3

To access the fields you need to use $this and -> operator

return $this->defaultPage;
Sign up to request clarification or add additional context in comments.

10 Comments

@Sujit Singh: I'm sorry, your change is terribly wrong (from terminology point of view), rolled back
@zerkms : i guess, i was right. See, in a function if we want to refer any variable, (say, for example $in) which is declared inside that function, we will access directly like $in, but if that variable is declared outside function, but, naturally, inside controller, we have to use $this->variable_name to access it. Am I right ?
@Sujit Singh: there is no "inside" function and "outside" function. There are fields and methods. Please use the correct terminology. You're right technically, but the terms you've just invented are terrible.
@zerkms : so you mean, instead of terms "variable" and "function", i should have used "fields" and "methods" ?
@Sujit Singh: php.net/oop - they are the basic terms of php's (and any other imperative programming language) Object Model. So, yes, the "class' function" is a method, the instance variable - is a field (or property). Though in some programming languages property and field mean different things.
|

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.