What is the best way to pass values to the attributes

7

I'm studying object-oriented programming. I am very fascinated as this new world.

But now a question has arisen. I believe you can help me.

I'm making a Person class, which has the attributes, age, and gender attributes. But this class will have some methods, they are: register () display() delete() edit ()

I would like to know the correct way to pass values to the attributes and in sequence to register in the DB.

PS. To register in the database I am making a class to work only with the bank. This bd class will have the method inserted.

If it is using "Set" and then "Get" to access the value of each attribute?

Use "construct" to assign values?

Or in the register () method it receives the values of the attributes and then passes these values to the attributes:

function cadastrar($nome, $idade, $sexo){
      $this->nome = $nome;
      $this->idade = $idade;
      $this->sexo = $sexo;
}

I believe that my doubt is very simple for experienced developers, but this is a very important question for my study.

And if it does not ask a lot, how can I know the correct time to use GET and SET and when a method should receive "parameters" / attribute values?

    
asked by anonymous 26.08.2015 / 22:26

4 answers

2

Object Oriented Programming

Object

  

"Object is anything that exists in the real world, in   concrete or abstract format, that is, that it exists physically   or just conceptually. "

Built-in object

  • data structure: characteristics (data) that are attributes
  • behavior: functions that are methods

Manipulation

  

"The object must handle methods and attributes belonging only to it"

Example

class Pessoa{

    private $nome;
    private $idade;

    public function __construct($nome, $idade){
        $this->nome = $nome;
        $this->idade = $idade;
    }

    public function setNome($novoNome){
        $this->nome = $novoNome;
    }

    public function getNome();

    public function getNomeDoMeio();

}

What is not object orientation

Objects that are related, that should be put as attributes, but that are placed as agents.

Example

class Endereco{
    protected $rua;
    protected $bairro;

    public function setRua($rua){
        $this->rua = $rua;
    }
}

class Pessoa extends Endereso{

    private $nome;
    private $idade;

    public function __construct($nome, $idade, $rua, $bairro){
        $this->nome = $nome;
        $this->idade = $idade;
        $this->rua = $rua;
        $this->bairro = $bairro;
    }
}

In your system you can define that "every person has an address", but this does not mean that it should extend / contain an address, the person is a person regardless of whether or not they have an address.

How to relate objects

Some objects

  • Reptiles
  • Mammals
  • Animal
  • Whale
  • Lizard

Relationship

  • Animal - > Mammal - > Whale
  • Animal - > Reptiles - > Lizard

In other words, the animal class must contain generic methods and attributes to hold both classes, but nothing specific for a given class.

Handling Attributes

The attributes are pertaining to the class ie within the class voce can and should handle them freely, but when an extender object wants capture or change its value this should use the appropriate methods, which are not necessarily the get or set.

Example

class Conta{

    private $saldo;

    public function getSaldo();

    public function depositar($valor){
        $this->saldo += $valor;
    }

    public function retirar($valor){
        if ($this->saldo >= $valor){
            $this->saldo = $this->saldo - $valor;
        }else{
            echo "Saldo insuficiente!!!";
        }
    }
}

Conclusion

Consider the attributes of each class well and always keep them as private or protected creating appropriate methods for its manipulation.

OBS

  

"Going from orientation to Object and doing anything 'alike' is easy." - Professor

  • My falconry material.
27.08.2015 / 03:05
1

One thing does not exclude the other.

But at some point you'll need to get the attributes of an object or assign new values to it . For this, using the concept of construct , you should leave the attributes as private and use encapsulamento .

    
26.08.2015 / 22:31
1

The best way is the one that fits the situation:).

  • Arguments passed in construct r have the advantage of already initializing one or more dependencies of the created object, this ensures that the attribute will no longer be modified (as long as it is not public or has not another method that modifies its state).

  • Using setters means that the values are optional and can be called at any time and the operation can be performed at any time, ie the value of that attribute can be changed during the life of the object.

26.08.2015 / 22:36
0

The construct you user to create a default starts, example zeroing the desired attributes.

The setters and getters you user in need to change and / or capture a specific attribute, a hint leave the attributes of the class private

But as you did it is also possible to create classes to insert more than one attribute at a time, making it easier to insert

    
26.08.2015 / 22:39