Discussions in OOP for beginners


#1
function wc_paytmg_dependency_deactivate() {
	require_once(WC_PAYTMG_INC.'helpers/class-deactivator.php');
	Wc_Paytm_Gateway_Deactivator::dependency_deactivate();
}

i was studying some code to understand as much as I can, but i ended up not understanding this part:

Wc_Paytm_Gateway_Deactivator::dependency_deactivate();

Wc_Paytm_Gateway_Deactivator → what actually is this in terms of code?
dependency_deactivate(); → I think this is some function, Right?


#2

It looks like the file is using implements. Not entirely sure. But implements can be used as a strict layout. Though from the looks of it, I just think it’s a placeholder for something.

That seems to just be the class name.

In OOP (all OOP languages), once a function is placed inside of a class it is no longer considered a function, but referred to as a method. So therefore, the method you are seeing is still a function, but you no longer call it a function. You call it a method.


ON A SIDE NOTE


You shouldn’t use static methods/ functions. When you use static methods/functions, you lose some of the OOP part such as inheritance.


#3

I have also read _constructor from many places, but I am still not able to understand it and its utility. Can you invest sometime to explain me in as simple word as possible so that I can grasp the concept.


#4

read_constructor is a custom function created by the author. You would have to ask them about that. Basically, custom functions are functions that the author makes. These functions can be named anything. So I can only do guess work as to what it does based on the function name.


#5

I was talking about

_constructor

function


#6

Can some one please help me to change the topic to “Some great PHP discussions in oops for beginners” Please?


#7

I assume you mean __construct. That method is indeed called the constructor, because it is called when the object is constructed.

The point of a constructor is to supply initial values for the fields within the object. Some people do all sort of stuff in constructors like calculations or connecting to a database, but that’s not really good form as that makes the class way harder to test.

Anyway, as a small example:

class Person
{
    private $name;

    public function __construct(string $name)
    {
         $this->name = $name;
    }

    public function getName(): string
    {
        return $this->name;
    }
}

Now if you want to create a new instance you could do new Person('John Doe') which would give you an object with name John Doe. This name cannot be modified later, because the object has not method to change it. Also note that you must pass a name for a Person to be constructed. If you do new Person() you will get an error because you didn’t supply a name.

This entire principle of having a variable inside a class and making the class fully responsible for it is called encapsulation.


#8

No it’s not, there is no implements in the class declaration. Maybe there is some sort of implicit contract created somewhere, but that’s just a Bad Idea ™.

Also, it’s not a strict layout as you call it, but an enforced contract used for polymorphism.

OT: The proper term is ‘The class appears to implement an interface’

Never say never. Static methods have their place, but indeed when starting out it’s hard to make that distinction and you’re probably better off avoiding them.


#9

This is very confusing for me?

public function __construct(string $name)

since string $name has been passed is this why it becomes mandatory to pass a name?
I have huge difficult understanding_construct function.


#10

Yes, the parameters you pass to new Person() will actually be passed to the __construct method internally.

So when I type new Person('John Doe') PHP will actually execute __construct('John Doe') internally.


#11

The passing of John Doe or any other name is mandatory?


#12

As written yes. Passing of a name is required. PHP does support optional arguments in which case it would not be required:

class Person {
    public function __construct($name = null) {

Is this your code you are trying to understand or some third party library or even a tutorial? I searched for it but nothing came up. The class naming convention implies that it is very old code. Perhaps not the best learning tool.

In any event, skim through the php oop docs and maybe things will make a bit more sense.


#13

I think the code is not old, but developer hasn’t used best practice. Here is the code file attached.wc-paytm-gateway.1.2.zip (25.3 KB)

Sometime I find php.net difficult to understand a s beginner.


#14

My sympathies. Supporting legacy code can be a challenge.

Which is why I suggested you skim it as opposed to reading in depth. And then go back again as things progress. Eventually it will start to make sense. Or else you will move on to a different job.


#15

You can use any name you like. John Doe is just an example.

It is the name used in English for a person you don’t know the name of, kind of a placeholder name