X
X
xinn2011-02-15 16:10:29
PHP
xinn, 2011-02-15 16:10:29

Differences between class and object?

To my shame, I suddenly realized that I did not understand the difference between a class and an object.
Those. purely theoretically, I understand that an object is an instance of a class, i.e. a class is like a blank, and already an object is already a “thing” with which you can work directly.
But then why (as far as PHP is concerned) can I still work with class methods ( ClassName::MethodName() ) as well as object methods ( $c = new ClassName; $c->MethodName() ), although no object is created in the first case.
Or, in this case, is a class just like a group of global functions in its "namespace" that do not require "personalization" by setting unique values ​​for their properties?
For some reason, a search in Google did not help, basically analogies of the form “Class - drawings of the machine, object - the machine itself” are given, but not a word about practical application.
Thanks in advance for the clarifications, otherwise this mess in my head is already starting to strain =)

Answer the question

In order to leave comments, you need to log in

7 answer(s)
W
Weageoo, 2011-02-15
@xinn

Class = a way to organize fields, methods, etc. = an encapsulated "piece" of functionality, a description of the structure of future objects + its own namespace inside.
The static component of a class - static methods and static fields - is a logical way to organize the functionality common to any object of a dedicated entity. Here the logical component matters - for example, the static Parse method is present in both the int class and the Guid class - but its functionality is different and corresponds to the selected entity. For the static component of a class, in principle, it can be thought of as a namespace. But a namespace cannot be initialized, and a namespace object cannot be created.
Object = an instance of a class = an object whose type is some class = a class after initialization = an entity in memory that has behavior that can change its state.

H
homm, 2011-02-15
@homm

You may find it easier to understand if you think of a class as a collection of functions and an object as a collection of data.
Some functions in a class can only be used with a dataset (i.e. an object) that is passed implicitly when you specify $object->method().
Other functions do not require an object, so they can be called directly from the class.
From this point of view, Python looks very logical, where any method has an explicit self argument that points to the object from which the method was called. And the entry object.method(5)is a short entry ObjectClass.method(object, 5).

S
Sergey Beresnev, 2011-02-16
@sectus

I came up with my stupid analogy.
Class is a god that is always there. And God, in his own image and likeness, creates specimens - humans. Each person is endowed with his own set of properties (height, weight, ...) and methods (swim, walk, fly, ...). And God, in turn, is endowed with methods common to all: accept prayers, punish, send manna from heaven and create a miracle. These methods can be private. Those. God, for example, can only accept the prayers of people, not Martians. But he can create a miracle both for people and for Martians - he does not feel sorry.
:)

R
Rafael Osipov, 2011-02-15
@Rafael

A class is a drawing, a class object is a machine assembled according to this drawing.
A static method is some kind of device (let it be a GPS navigator in our case) that comes with a blueprint and is present in every car built according to this blueprint. This device does not involve the machine in any way, and works by itself.
You can run with a GPS navigator without building a car.
If we move away from the popular presentation, then static methods are not associated with class instances. And they can be called regardless of whether an instance of the class is created or not.

C
Chvanikoff, 2011-02-15
@Chvanikoff

Without class implementation (object creation) you can only work with static methods/properties of the class. If on the example of the mentioned machine, the class describes what it is, and the implementation (instance) of the class, respectively, implements some specific machine.
The simplest miniature example:

class Car {
    public $brand = NULL;
}
$car = new Car;
$car->brand = 'VW';
$car2 = new Car;
$car2->brand = 'Жигули';

As a result, we have 2 cars — VW and Zhiguli. Both are instances of the Car class. Without creating an instance of the class, you will not set the brand - there is nothing to set it, in fact.
Well it if primitives on fingers.
In general, here you go: about OOP on php.su

1
1nd1go, 2011-02-15
@1nd1go

In the simplest case: A class is a definition of common behavior for the objects that this class generates. The object will have the behavior specified by this class and differ from other objects in its state (well, if the state is the same, then it will simply be like two identical cars - two entities are different, but they look the same).
Further, static methods and fields can be added to the class. Here, the class itself will have the ability to store its state and perform actions. In most cases (but not in all) it is required to control the production of objects of this class (Singleton pattern, Factory, etc.).
Even further, in an object-oriented language, everything is an object, so a class can also be an object. This is necessary in order to control the classes themselves, load them while the program is running or starting, etc.
All this understanding will come with the experience of writing programs, you just need to think in metaphors.

A
Alexey Sidorov, 2011-02-15
@Gortauer87

Everything is simple. In the case of a class, this is just some description of methods, fields, etc. When we call any class method, we implicitly pass the this pointer in the arguments.
That is, in reality, the call
a->b(arg);
looks like
b(arg,a);
And static methods differ from ordinary ones in that they do not have this implicit this argument.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question