Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php  | 
            ||
| 11 | abstract class DAO implements DAOInterface { | 
            ||
| 12 | |||
| 13 | /** @var \PDO */  | 
            ||
| 14 | protected $pdo;  | 
            ||
| 15 | |||
| 16 | /** @var string[] */  | 
            ||
| 17 | protected $selectCollumns = ['*'];  | 
            ||
| 18 | |||
| 19 | /**  | 
            ||
| 20 | * Retorna um objeto a partir da linha da tabela  | 
            ||
| 21 | * @param array[] $row  | 
            ||
| 22 | */  | 
            ||
| 23 | abstract protected function mapObject($row);  | 
            ||
| 24 | |||
| 25 | /** Retorna a linha da tabela a partir de um objeto  | 
            ||
| 26 | * @param object $obj  | 
            ||
| 27 | */  | 
            ||
| 28 | abstract protected function mapRow($obj);  | 
            ||
| 29 | |||
| 30 | /** Inicia o DAO */  | 
            ||
| 31 | 	public function __construct() { | 
            ||
| 34 | |||
| 35 | /**  | 
            ||
| 36 | * Define uma conexão manualmente  | 
            ||
| 37 | * @param \PDO $pdo  | 
            ||
| 38 | */  | 
            ||
| 39 | 	public function setPDO($pdo) { | 
            ||
| 42 | |||
| 43 | /**  | 
            ||
| 44 | * Define quais colunas serão consultadas nos comandos SELECT  | 
            ||
| 45 | * @param string[] $collumns  | 
            ||
| 46 | */  | 
            ||
| 47 | 	public function setSelectCollumns(array $collumns) { | 
            ||
| 50 | |||
| 51 | /**  | 
            ||
| 52 | * Adiciona nova coluna no select  | 
            ||
| 53 | * @param string $collumn  | 
            ||
| 54 | */  | 
            ||
| 55 | 	public function addSelectCollumn($collumn) { | 
            ||
| 60 | |||
| 61 | /**  | 
            ||
| 62 | * Salva o registro  | 
            ||
| 63 | * @param object $obj  | 
            ||
| 64 | */  | 
            ||
| 65 | 	public function save($obj) { | 
            ||
| 76 | |||
| 77 | /** Insere o registro */  | 
            ||
| 78 | 	protected function insert() { | 
            ||
| 90 | |||
| 91 | /** Atualiza o registro */  | 
            ||
| 92 | 	protected function update() { | 
            ||
| 108 | |||
| 109 | /**  | 
            ||
| 110 | * Exclui o registro  | 
            ||
| 111 | * @param object $obj  | 
            ||
| 112 | */  | 
            ||
| 113 | 	public function delete($obj) { | 
            ||
| 116 | |||
| 117 | /**  | 
            ||
| 118 | * Exclui o registro por id  | 
            ||
| 119 | * @param int $id  | 
            ||
| 120 | */  | 
            ||
| 121 | 	public function deleteById($id) { | 
            ||
| 129 | |||
| 130 | /**  | 
            ||
| 131 | * Busca o objeto pelo id  | 
            ||
| 132 | * @param int $id  | 
            ||
| 133 | */  | 
            ||
| 134 | 	public function fetchById($id) { | 
            ||
| 137 | |||
| 138 | /**  | 
            ||
| 139 | * Busca o objeto por um campo/atributo específico  | 
            ||
| 140 | * @param string $name Nome do atributo  | 
            ||
| 141 | * @param mixed $value Valor do atributo  | 
            ||
| 142 | */  | 
            ||
| 143 | 	public function fetchByField($name, $value) { | 
            ||
| 146 | |||
| 147 | /**  | 
            ||
| 148 | * Busca o objeto  | 
            ||
| 149 | * @param string[] $filter Array de filtros  | 
            ||
| 150 | * @param string $option [Order by, Limit, etc]  | 
            ||
| 151 | */  | 
            ||
| 152 | 	public function fetch($filter, $option = '') { | 
            ||
| 166 | |||
| 167 | /**  | 
            ||
| 168 | * Retorna todos os registros  | 
            ||
| 169 | *  | 
            ||
| 170 | * <code>  | 
            ||
| 171 | * $dao->fetchAll( ['id = ?' => 10]);  | 
            ||
| 172 | * </code>  | 
            ||
| 173 | * @param string[] $filter Array de filtros  | 
            ||
| 174 | * @param string $option [Order by, Limit, etc]  | 
            ||
| 175 | */  | 
            ||
| 176 | 	public function fetchAll($filter = [], $option = '') { | 
            ||
| 194 | |||
| 195 | /**  | 
            ||
| 196 | * Retorna comando SELECT  | 
            ||
| 197 | * @param string $selectCollumns  | 
            ||
| 198 | * @return string  | 
            ||
| 199 | * @example "SELECT * FROM user"  | 
            ||
| 200 | */  | 
            ||
| 201 | 	protected function selectSQL($selectCollumns = ['*']) { | 
            ||
| 204 | |||
| 205 | /**  | 
            ||
| 206 | * Retorna comando WHERE  | 
            ||
| 207 | * @param string[] $filter  | 
            ||
| 208 | * @return string  | 
            ||
| 209 | */  | 
            ||
| 210 | 	private function whereSQL(&$filter) { | 
            ||
| 214 | |||
| 215 | /**  | 
            ||
| 216 | * Retorna o total de registros  | 
            ||
| 217 | * @param string[] $filter Array de filtros  | 
            ||
| 218 | * @return int  | 
            ||
| 219 | */  | 
            ||
| 220 | 	public function numRows($filter = []) { | 
            ||
| 236 | |||
| 237 | /**  | 
            ||
| 238 | * Retorna True se objeto existir  | 
            ||
| 239 | * @return boolean  | 
            ||
| 240 | */  | 
            ||
| 241 | 	protected function objExists() { | 
            ||
| 244 | |||
| 245 | /** Define como Página 404 se o objeto não existir */  | 
            ||
| 246 | 	public function validateObject() { | 
            ||
| 251 | |||
| 252 | }  | 
            ||
| 253 | 
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: