PHP Getter ve Setter vs Constructor m

533388

Hectopat
Katılım
27 Mart 2022
Mesajlar
4.385
Makaleler
2
Çözümler
54
Yapıcı metot kullanılınca direkt olarak istenilen şey obje çalışır çalışmaz geliyor ama getter ve setter'da yine çağırmamız lazım. Getter ve setter nerede kullanabilirim?
 
Son düzenleyen: Moderatör:
C++ icin ornek vereyim, ayni class icersinde kullanman lazim. Gecenlerde soyle cok basit ve temel bir terfi kodu yazmistim. PHP'de nasil isliyor bilmiyorum tabi ama mantik cok da farkli degildir herhalde.

C++:
#include <iostream>
using namespace std;

class AbstractEmployee { //Abstract
    virtual void AskForPromotion() = 0;
};

class Employee : AbstractEmployee{
    //Encapsulation
    string Name;
    string Department;
    string Relation;
    int Experience;
    int Age;

public:
    void setName(string name){
        Name = name;
    }
    string getName(){
        return Name;
    }

    void setDepartment(string department){
        Department = department;
    }
    string getDepartment(){
        return Department;
    }

    void setRelation(string relation){
        Relation = relation;
    }
    string getRelation(){
        return Relation;
    }

    void setExperience(int experience){
        Experience = experience;
    }
    int getExperience(){
        return Experience;
    }

    void setAge(int age){
        if(age >= 18)
        Age = age;
    }
    int getAge(){
        return Age;
    }
    void IntroduceYourself() {
        cout << "Name: " << Name << endl;
        cout << "Department: " << Department << endl;
        cout << "Relation: " << Relation << endl;
        cout << "Experience: " << Experience << endl;
        cout << "Age: " << Age << endl;
    }
    Employee(string name, string department, string relation, int experience, int age) {
        Name = name;
        Department = department;
        Relation = relation;
        Experience = experience;
        Age = age;
    }
    void AskForPromotion() {
        if(Age >= 25 && Relation == "Good" && Experience >= 3)
        cout << Name << " , you got promoted! Yaaay." << endl;
        else
        cout << "Sorry " << Name << " , your promotion is denied..." << endl;
    }
};

int main(){
    Employee emp1 = Employee("Gorkem", "IT", "Good", 2, 27);
    Employee emp2 = Employee("Furkan", "Web", "Good", 3, 28);
    emp1.AskForPromotion();
    emp2.AskForPromotion();

    return 0;
}
 
Aslında bu constructor olmadığı zamanlarda değişkenlerin isimlerini ve özelliklerini değiştirip get ile alabildiğimiz bir yol mu?

C++'da bir class olusturdugun an sen elle yapmasan bile default olarak constructor olusur. Eger elle olusturdugun constructor icine parametre atamazsan kod hata bile verir mesela. Set ve get daha cok class icerisindeki objeler private veya protected ise kullaniliyor. Oyle olunca dogrudan ulasamiyorsun o sebeple setter ve getter kullanman lazim. PHP'de mantik bu sekilde mi isliyor bilmiyorum.
 
Getter ve setter mantığı, constructordan bağımsız. Private değişkenleri erişilebilir hale getiriyorsun.

Getter ve setterlar accessors olarak adlandırılıyor.
Amaç bir değişkenin iç gösterimini saklamak yada daha açıklayıcı isimlerle kullanıcılara aktarmak olabilir.
Belirli durumlarda setter'i devre dışı birakarak kullanıcının degistirmesini engellemek olabilir...

Constructor ise nesneyi yarattığın zaman nesneyi başlatacak şey. Tanımlanması gereken değerleri tanımlar, nesnenin düzgün çalışması için yapılması gereken şeyleri yapar. Böylece nesneyi oluşturan kişi hatayla karsilasmaz.

Ikisi bambaşka şeyler kısaca. x ve y değişkenlerini ilk defa set eden sey constructor olur ama x yada y'yi bir yerde kullanmak istersen getter kullanır, değişiklik yapmak istersen setter kullanırsın.
 
Evet bugün onu daha iyi anladım. Getter ve setter private değişkenlerde kullanılıyor ama constructor ise obje oluşur oluşmaz başlıyor.
 

Geri
Yukarı