Skip to content

Instantly share code, notes, and snippets.

@dozer111
Last active March 23, 2020 20:41
Show Gist options
  • Save dozer111/e046291bd4891e973d1866cfb3c927a8 to your computer and use it in GitHub Desktop.
Save dozer111/e046291bd4891e973d1866cfb3c927a8 to your computer and use it in GitHub Desktop.
addConst

Сорри если боян, я только недавно увидел такое:

Прием: "вспомогательный конструктор"

Как я писал до этого

class Student {  
  const TYPE_OCHN = 1;  
 const TYPE_ZAO = 2;  
  
 private $name;  
 private $sur;  
 private $birth;  
 private $type;  
  
 public function __construct($name,$sur,$type,$birth = null)  
 {  /// допустим, что тут валидация аргументов  
  /// .... 
  /// ....  
  $this->name = $name;  
  $this->sur = $sur;  
  $this->type = $type;  
  $this->birth = $birth;  
  }  
  
  /// какой-то код для работы дальше  
}  
# из всего примера нам важно, на какую именно форму обучения поступает student  
$student1 = new Student('Alex1','Sur11',Student::TYPE_OCHN);  
$student2 = new Student('Alex2','Sur12',Student::TYPE_OCHN);  
$student3 = new Student('Alex3','Sur13',Student::TYPE_OCHN);  
$student4 = new Student('Alex4','Sur14',Student::TYPE_OCHN);  
$student5 = new Student('Alex5','Sur15',Student::TYPE_ZAO);  
$student6 = new Student('Alex6','Sur16',Student::TYPE_OCHN);  
$student7 = new Student('Alex7','Sur17',Student::TYPE_ZAO);  
$student8 = new Student('Alex8','Sur18',Student::TYPE_OCHN);

А теперь та же задача)

class Student {  
  const TYPE_OCHN = 1;  
 const TYPE_ZAO = 2;  
  
 private $name;  
 private $sur;  
 private $birth;  
 private $type;  
  
 public function __construct($name,$sur,$type,$birth = null)  
 {  /// допустим, что тут валидация аргументов  
 /// .... /// ....  
  $this->name = $name;  
  $this->sur = $sur;  
  $this->type = $type;  
  $this->birth = $birth;  
  }  
  
  # приём вспомогательного конструктора  
  public static function createOchn($name,$sur,$birth = null)  
 {    
     return new self($name,$sur,self::TYPE_OCHN,$birth);  
 }  
  
  public static function createZao($name,$sur,$birth = null)  
 {  
   return new self($name,$sur,self::TYPE_ZAO,$birth);  
 }  
  
  /// какой-то код для работы дальше  
}  
# это воистину гениально!  
$student1 = Student::createOchn('Alex1','Sur11');  
$student2 = Student::createOchn('Alex2','Sur11');  
$student3 = Student::createOchn('Alex3','Sur11');  
$student4 = Student::createZao('Alex4','Sur11');  
$student1 = Student::createOchn('Alex5','Sur11');  
$student1 = Student::createZao('Alex6','Sur11');  
$student1 = Student::createZao('Alex7','Sur11');  
$student1 = Student::createOchn('Alex8','Sur11');  
$student1 = Student::createOchn('Alex9','Sur11');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment