Setter and getters pHP

In the above the public function set_name($value) {
}

set_ is a keyword that compiler recognize or it is just like an ordinary function/method?

It’s probably some configuration in your editor.

get/set are not keywords. You can pretty much call you functions whatever you want. Only names that start with __ are reserved. Things like __construct.

The use of getter/setters is quite common but in many cases they really should be avoided. In your example, something like:

class Product
{
    private $name;
    public function __construct($name) {
        $this->name = $name;
    }
}
$product = new Product('Birdhouse');

Not only eliminates the need for a setter but ensures that all product will have a name. It also prevents the name from being inadvertently changed. Just something to think about.

1 Like

__construct in a way instantanize right?

There is a __set magic method in PHP.
But in this case it is just part of an ordinary method name.

1 Like

Yep. Take a moment and skim quickly through http://php.net/manual/en/language.oop5.php. Then, as you progress, go back and read individual sections such as http://php.net/manual/en/language.oop5.decon.php in detail.

1 Like

__construct is another magic method.
It will be called automatically when the object is instantiated, so you don’t need to call the setter (or any other method you wan’t to put in the construct method).

1 Like

But I believe that there will be some advantage/utility of getters and setters over the __construct function.

In some cases you may not want to set those properties for all instances of an object immediately at creation.
Using setters instead of construct will give you the choice of if and when to set a property, that may be your advantage. But construct has the advantage of you not needing to call those methods.
The best method really depends upon your intentions.

1 Like

Of course. Just about everything in programming involves trade offs of one sort or another. If you search around a bit you will find many many debates on the pros and cons of getters/setters.

For example, your original code is functionality identical to:

class Product {
    public $name;
}

So at this point, you would have to decide on exactly what advantage adding getter/setters will actually give you. Try to avoid future “what ifs” and focus on what your current app actually needs. Enjoy.

Thank you so much this clarifies the doubt.

Right. “what If’s” have no end. And no code is air-tight to take care of any possibility that may arise in the future.

1 Like

__construct by default are considered as static properties/method in behaviour and accessed by :: instead of this: ->?

__construct() is a method, not a property.

corrected!

You don’t call __construct() yourself, except when you need to call the parent class’ constructor from the current constructor and that call is defined as parent::__construct(), which is not related to static/non-static calls (simply because you cannot call an overwritten method other than using parent::)

As SamA74 mention in an earlier post, __construct is automatically called when you new an object.

The only time it would be called (aka accessed) by you is if you were extending a class and needed to call the parent constructor from inside the child constructor.

And again, this is all documented: http://php.net/manual/en/language.oop5.decon.php

1 Like

But we use double colon, which is used for static.

except when we use parent::

example:

<?php

error_reporting(E_ALL);

class A
{
    public function foo()
    {
        return 'a';
    }
}

class B extends A
{
    public function foo()
    {
        return 'b' . parent::foo();
    }
}

$a = new A;
echo $a->foo(); // 'a'
echo $a::foo(); // Deprecated: Non-static method A::foo() should not be called statically

$b = new B;
echo $b->foo(); // 'ba'
echo $b::foo(); // Deprecated: Non-static method B::foo() should not be called statically
3 Likes

Just out of Friday boredom:

<?php
error_reporting(E_ALL);

class Product
{
    private $name;
    public function __construct($name)
    {
        $this->name = $name;
    }
}
$product = Product::__construct('Rebecca');

Results in:

PHP Fatal error:  Uncaught Error: 
Non-static method Product::__construct() 
cannot be called statically in product.php:12