class
new
->
$this
Objects are key to understanding object-oriented technology. You can find many examples of real-world objects: your dog, your desk, your television set, your bicycle.
Real-world objects share two characteristics: they all have state and behavior. Dogs have state (name, color, breed, hungry) and behavior (barking, fetching, wagging tail). Bicycles also have state (current gear, current pedal cadence, current speed) and behavior (changing gear, changing pedal cadence, applying brakes). Identifying the state and behavior for real-world objects is a great way to begin thinking in terms of object-oriented programming.
Bundling code into individual software objects provides a number of benefits, including:
- Modularity: The source code for an object can be written and maintained independently of the source code for other objects. Once created, an object can be easily passed around inside the system.
- Information-hiding (encapsulation): By interacting only with an object's methods, the details of its internal implementation remain hidden from the outside world.
- Code re-use: If an object already exists (perhaps written by another software developer), you can use that object in your program. This allows specialists to implement/test/debug complex, task-specific objects, which you can then trust to run in your own code.
- Pluggability and debugging ease: If a particular object turns out to be problematic, you can simply remove it from your application and plug in a different object as its replacement.
A class is a source code blueprint for objects. Other analogies are classes provide a factory to create objects with, or an "object template".
Objects are instantiated out of classes by using the new
keyword. Another way to think about it is
that we instantiate (create instances from) a class in order to assign an instance of the class to a variable.
The class
keyword takes a code block that represents an object factory. Inside the code block,
properties and methods are created.
Objects are instantiated out of classes by using the new
keyword. Here is a completely bare Person
class:
<?php
// Defines a "Person" class.
class Person {
}
// Creates a "Person" object.
$person = new Person();
The “class” blueprint specifies:
- Name: identifies the class.
- Properties (variables): contain the attributes of the class.
- Methods (functions): contain the behaviors of the class.
We might have a Person class that represents a person. The person might have properties such as a name, a height, a weight, a date of birth, etc. A person might have some behaviors, like walking, talking, which we could represent as methods.
We can access properties or methods of the object using the object operator, ->
<?php
// Defines a "Person" class.
class Person {
// Defines a method.
function sayHi() {
echo 'Hi!';
}
}
// Creates a "Person" object.
$person = new Person();
$person->sayHi(); // Hi!
Within an object method, $this
is a reference to the current object. Example:
<?php
class Person {
// Define a property.
public $name;
// Defines a "setter" method.
function setName($name) {
$this->name = $name;
}
function sayName() {
echo $this->name;
}
}
$person = new Person();
$person->setName('Joe Bob'); // Sets the name.
$person->sayName(); //Joe Bob
Objects are always passed by reference (rather than making a copy of it), meaning that a function that accepts an object as a parameter can modify the original object passed to it.
- Class name: uppercase camels, eg:
MyClass
- Class properties/methods: lowercase camels, eg:
myProperty
- One class per file.
- No closing
?>
tag.
- Create a “Person” class and instantiate an object of it.
- Add properties for name, age.
- Add methods getName(), getAge().
- Invoke var_dump() on the person object to inspect it.