S
S
serikd2016-04-17 00:28:36
PHP
serikd, 2016-04-17 00:28:36

Where is the best place to declare private properties?

Hello.
Tell me where you define private properties in the class.
At the beginning?

class A 
{
    private $items;

    public function doSomthing()
    {
        return 'I done';
    }

    ...

   public function implodeItems()
   {
       return implode($this->items);
    }
}

Or somewhere in the class when you enter a variable?
class A 
{
    public function doSomthing()
    {
        return 'I done';
    }

    ...
   
   private $items;

   public function setItems(array $value)
   {
       $this->items = $value;
   } 

   public function implodeItems()
   {
       return implode($this->items);
   }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
D', 2016-04-17
@serikd

Variables are usually declared at the beginning of the file (and it doesn't matter if it's a class or just a file with functions)

M
Maxim Yakupov, 2016-04-17
@DeeplessHole

At the beginning. The view may blur and miss the property declaration. And if they are all defined at the beginning, then, firstly, at the first glance at the class, it becomes clear what it operates with, and, secondly, if you forgot the property signature, then you just look at the beginning of the file and easily find it.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question