Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I declare a property inside a method?

I'm a totally newbie in class writing in PHP, started a couple days ago.

I'd like to declare a new "public property" inside a method to be used in other methods.

This is what I thought (Of course it doesnt' work!):

    class hello {

        public function b() {
            public $c = 20; //I'd like to make $c usable by the method output() 
        }

        public function output() {
            echo $c;
        }
    }

$new = new hello;
$new->output();

Thanks in advance for any tips.

like image 810
Giorgio Avatar asked Dec 09 '22 03:12

Giorgio


2 Answers

I'd like to declare a new "public property" inside a method to be used in other methods.

If the other methods are part of the same class, you don't need a public property, a private property will fit your needs. Private properties are accessible within the same class only which helps to keep things simple.

Also understand the difference between declaring a property and assigning a value to it. Declaring is done when the code is loaded, assigning when it executes. So declaring (or defining) a property (private or public) needs a special place in the PHP syntax, that is in the body of your class and not inside in a function.

You access properties inside the class by using the special variable $this in PHP.

The pseudo-variable $this is available when a method is called from within an object context. $this is a reference to the calling object (usually the object to which the method belongs [to]). From PHP Manual

Private property example:

class hello {
    private $c; # properties defined like this have the value NULL by default

    public function b() {
        $this->c = 20; # assign the value 20 to private property $c
    }

    public function output() {
        echo $this->c; # access private property $c
    }
}

$new = new hello;
$new->output(); # NULL
$new->b();
$new->output(); # 20

Hope this is helpful. You use a private property because everything else in your program does not need to care about it, so inside your class you know that nothing else can manipulate the value. See as well Visibility­Docs.

like image 187
hakre Avatar answered Dec 26 '22 12:12

hakre


Every variable of the class is public when you define it inside of a method (function)! You can do this the following way:

class hello {

    public function b() {
         $this->c = 20; 
    }

    public function output() {
        echo $this->c;
    }
}

$new = new hello;
$new->output();

or let function b() return $c and then pass it as a variable to output():

class hello {

    public function b() {
         return $c = 20; 
    }

    public function output($c) {
        echo $c;
    }
}

$new = new hello;
$c = $new->b();
$new->output($c);

Remember all variables inside a function are ONLY accessable from within that particular function...unless you use $this of course, which makes the variable a class property! Also, it's recommended to only return variables...echo is only for the real output, the pure HTML, the template, your view, if you know what I mean :)

like image 26
Anonymous Avatar answered Dec 26 '22 13:12

Anonymous