Se quiser só garantir o tamanho crie e use uma função que faça isto sempre que quiser garantir o tamanho:
function fixedSTring($txt, $length) {
$length = max($length, strlen($length);
return str_pad(substr($txt, 0, $length), $length);
}
Se quiser algo garantido e automático, basta criar um novo tipo, uma nova classe que além de guardar uma string, guarde o tamanho fixo dela e aplique o critério acima. Algo assim (não está completo e e testado, é só uma base)
final class FixedString {
private $txt = '';
public function __construct($txt, $length) {
$length = max($length, strlen($length));
$this->txt = str_pad(substr($txt, 0, $length), $length);
}
public function __toString() {
return $this->txt;
}
}
Uso:
$texto = new FixedString("teste", 20);
Coloquei no GitHub para referência futura.
Evidentemente precisa fazer melhores verificações, ter mais métodos que ajudem o trabalho. Fazer um tipo novo deve ser muito bem pensado. Eu não faria o preenchimento e corte da string de forma automática assim, tem maneiras melhores de lidar com isto, mas a pergunta não entra em detalhes.
$variavel = 'minha_string'
. Como já disse em outra resposta: O php é uma linguagem de tipagem fraca.