How best to pass values to attributes

Asked

Viewed 1,466 times

7

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

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

I’m doing a Person class, which has the attributes name, age and sex. But this class will have some methods, they are: register() display() erase() edit()

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

PS. To register in the database I am making a class to work only with the database. This class of the comic, will have the insert method.

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

Use "Construct" to assign values?

Or in the method register() it receiving the values of the attributes and then pass 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 issue for my study.

And if it’s not too much to ask, how can I know the right time to use GET and SET and when a method should receive "parameters"/attribute values ?

  • 1
  • 4

    Welcome to the world of Object Orientation! It’s cool this interest in learning a paradigm that is so used nowadays! Do not be shy in asking questions: a person who asks is silly for 5 minutes. The one who does not ask can be foolish for the rest of life.

  • 1

    True, today I may be breaking my head to learn the concepts, but you can be sure that tomorrow I will be understanding and helping those next to you who are in doubt. Thank you @Pedrolaini

  • Thanks @rray. Already helping a lot what you recommended me!

  • Poor guy, another one who fell for the OOP story :)

4 answers

2


Object-Oriented Programming

Object

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

Object incorporates

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

Manipulation

"The Object must manipulate 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 have relation, that must be put as attributes, but that are positions as dependents.

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 that does not mean that it should extend/contain an address, the person is a person whether or not he or she has an address.

How to relate objects

Some objects

  • Reptiles
  • Mamifero
  • Animal
  • Whale
  • Lizard

Relationship

  • Animal -> Mamifero -> Whale
  • Animal -> Reptiles -> Lizard

That is, the animal class must contain generic methods and attributes to accommodate both classes, but nothing specific for a particular class.

Manipulating attributes

The attributes belong to the class or within the Voce class can and should manipulate freely, but when an Extendo object wishes capture or change its value should use the appropriate methods, that 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!!!";
        }
    }
}

Completion

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

OBS

"Leaving the Object orientation and doing something similar is easy." - Professor

  • Stuff from my falcon.
  • It is more common to see Pessoa extends BancoDeDados { hehe.

  • @True rray, nothing to do with one another.

  • 1

    who said a person can’t be a database ? kkkkk

1

One does not exclude the other.

You pass the construct facilitates the creation of an object, Since you don’t need to create the object and then set the parameters in it, you can do it directly in the Construct.

But eventually you’re gonna need take attributes of an object or assign new values to it. To this end, using the concept of encapsulamento, it is good you leave the attributes as private and use the setters and getters.

  • So if I understand correctly, I should always use GET and SET when the attribute is private ?

  • And if you use Construct receiving the values that I will only use for the registration (name, age, gender), will it "get in the way" when I use the delete() method? because in it I do not see the pq pass name, age and sex.

  • Yes, because when you say the attribute is private, other classes cannot access it. Setters and getters are public methods used to access private variables. This concept is called encapsulation, when you restrict access to variables only by public methods

  • Note: The Construct is used to initialize the object. You can use several different constructs in the same class depending on your need. if you can call object.register( ), object.delete( ) I see no need to pass parameters to them, since the object itself that is calling already has the attributes.. can be accessed using $this->targetName

  • and if you have used another class for these methods (which is more indicated), called CRUD (create, Restore, update, delete), you usually pass the whole object as parameter to them... and within the method you access the object attributes when you need to

  • wow, I thought there could be only one Construct per class. When there is more than one, how should I do? kind as name for it and its values?

  • 1

    In php you don’t have Overload, It is possible to create classes with two constructors? @leandroMacedo

  • I didn’t know that php wasn’t possible. So forget that for now. But there are several concepts in Object Orientation that you need to study that will facilitate the understanding of these things further, such as: Inheritance, Polymorphism, Encapsulation, Overload of methods, Superscript of methods, etc

Show 3 more comments

1

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

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

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

0

The Construct you use to create a start pattern, example reset the desired attributes.

The setters and the getters you user in need of changing and/or capturing a specific attribute, a tip leave the attributes of the private class

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.