In a nutshell, a Class is a blueprint for an object. And an object encapsulates conceptually related State and Responsibility of something in your Application and usually offers an programming interface with which to interact with these. This fosters code reuse and improves maintainability. Show Imagine a Lock:
Ignore the The Lock class is a blueprint for all the Locks in your
application. A Lock can either be locked or unlocked, represented by the property
Let's create another lock, also encapsulating it's own state
but because each object/instance encapsulates it's own state, the first lock stays locked
Now the entire reponsibility of keeping a Lock either locked or unlocked is encaspulated within the Lock class. You don't have to rebuilt it each time you want to lock something and if you want to change how a Lock works you can change this in the blueprint of Lock instead of all the classes having a Lock, e.g. a Door:
Now when you create a Door object you can assign a Lock object to it. Since the Lock object handles all the responsibility of whether something is locked or unlocked, the Door does not have to care about this. In fact any objects that can use a Lock would not have to care, for instance a Chest
As you can see, the reponsibility of the Chest is different from that of a door. A chest contains loot, while a door separates rooms. You could code the locked or unlocked state into both classes, but with a separate Lock class, you don't have to and can reuse the Lock.
Chest and Door now have their unique locks. If the lock was a magical lock that can exist in multiple places at the same time, like in Quantum physics, you could assign the same lock to both chest and door, e.g.
and when you While I admit Quantum Locks are a bad example, it illustrates the concept of sharing of objects instead of rebuilding state and responsibility all over the place. A real world example could be a database object that you pass to classes using the database. Note that the examples above do not show how to get to the Lock of a Chest or a Door to use the Also check When to use self over $this? for a more in-depth explanation of Classes and Objects and how to work with them For some additional resources check
Apa yang dimaksud dengan class pada PHP?Class dalam analogi ini adalah sebuah gambaran umum dari sebuah benda. Didalam pemrograman nanti, contoh class itu seperti koneksi database dan profil user. Didalam PHP, penulisan class ini diawali dengan keyword class, kemudian diikuti dengan nama dari class.
Apa itu class dan method?Class merupakan suatu blueprint atau cetakan untuk menciptakan suatu instant dari object. class juga merupakan grup suatu object dengan kemiripan attributes/properties, behaviour dan relasi ke object lain. Contoh : Class Person, Vehicle, Tree, Fruit dan lain-lain. Method adalah kumpulan program yang mempunyai nama.
Apa yang dimaksud dengan class dan objek?Class adalah rancangan atau blue print dari sebuah objek. Sedangkan objek adalah sebuah variabel yang merupakan instance atau perwujudan dari Class.
Apa itu objek di PHP?"Object" adalah turunan dari sebuah class, dan Anda dapat membuat banyak instance dari class yang sama. Sebagai contoh, ada class Person tunggal, tetapi banyak object Person dapat menjadi instance dari class ini — dan , zainab , hector , dll. Class mendefinisikan properti.
|