In general, there was an argument about how to write a class. I pass parameters to class methods, for example

$myClass->search($str, $param1, $param2) 

and comrade says that all methods should be done without parameters, and everything should be taken from properties. Those. before calling the class method, set it

 $myClass->str = $str; $myClass->param1 = $param1; 

and cause

 $myClass->search(); 

and inside search already take these parameters from

 $this->str 

etc. This method seems to me very inconvenient, but the comrade (he is the boss))) motivates by the fact that various optional parameters will appear for the methods, and then the prototype recording and the call itself will look cumbersome.

Who writes how?

    2 answers 2

    Here it must be remembered that the class should contain only those members that really relate to the entity being modeled by it. To score it with other fields / properties only for the consideration that “recording the prototype and the call itself will look cumbersome” is not too good. However, transferring 100,500 parameters to a method is not the best idea. But the fact that your class is forced to either encapsulate these same 100,500 fields, or contain methods with the same bunch of arguments, usually only says that your class is not designed correctly, and, in all likelihood, takes on too much work, and therefore, it makes sense to break it up into somewhat less ponderous entities.

    but comrade (he's the boss)

    however, this valuable remark can nullify all the arguments in this dispute.

      The set () and get () interceptor methods allow you to work with properties fairly flexibly. This is very similar to what your boss says with some nuances.

       class Foo() { // В этом массиве мы будем хранить данные, // какие это будут данные - нам не важно private $_data = array(); __construct() {} // Добавляем данные в наш массив __set( $key, $val ) { $this->_data[$key] = $val; return $this; } // Читаем данные из массива __get( $key ) { return $this->_data[$key]; } search() {} } 

      It works like this:

       $bar = new Foo(); $bar->str( 'aaaaaa' ) ->param1( 'bbbbb' ) ->param2( 'cccc' ) ->search(); 

      You can improve the class by adding a check for the existence of data to avoid data rubbing or reading errors.