Class php mode extends

1

How can I print the cpf screen according to my code?

The code:

<?php

class valida {
    protected $cpf;

    public function cpf($cpf) {
        if(is_numeric($cpf) and $cpf > 11) {
            $this->cpf = $cpf;
            return $cpf;
        }
    }

}
class manda extends valida {
    function foo() {
        return $this->cpf;
    }
}

$cpf1 = $_GET['cpf'];

$p1 = new valida;
$p1->cpf($cpf1);

$sis = new manda;
echo $sis->foo();

What am I doing wrong?

    
asked by anonymous 05.06.2018 / 22:20

1 answer

0

$p1 has nothing to do with $sis , because they are different objects, although they have class hierarchy.

If you want the same CPF in $sis , you should assign it using cpf() ,

$sis = new manda;
$sis->cpf($cpf1);
echo $sis->foo();

It is clearer to understand class hierarchy if we think it serves for code economy in the child class,

class manda extends valida {
    public function foo($cpf) {
        // Aproveitando a implementação anterior de cpf().
        $cpf = cpf(100000000000 + $cpf);
        return $cpf;
    }
}

$sis = new valida;
echo $sis->foo($cpf1);

EDIT: Change% from% to

public function cpf($cpf) {
    if (is_numeric($cpf) && ceil(log10($cpf)) >= 11) {
        this->$cpf = $cpf + 0;
        return this->$cpf;
    }
}
    
05.06.2018 / 22:34