Verification: a143cc29221c9be0

Php class and object function

Php class and object function

Class Objects

An Object is an instance of a Class. A class is like a blueprint while an instance is a copy of the class with actual values. It’s not an idea anymore, it’s an actual dog, like a dog of breed pug who’s seven years old. You can have many dogs to create many different instances, but without the class as a guide, you would be lost, not knowing what information is required.
An object consists of : 

  • State: It is represented by the attributes of an object. It also reflects the properties of an object.
  • Behavior: It is represented by the methods of an object. It also reflects the response of an object to other objects.
  • Identity: It gives a unique name to an object and enables one object to interact with other objects.

python class

Declaring Objects (Also called instantiating a class)

When an object of a class is created, the class is said to be instantiated. All the instances share the attributes and the behavior of the class. But the values of those attributes, i.e. the state are unique for each object. A single class may have any number of instances.

Example:
 

python declaring an object

Declaring an object – 

Python3

class Dog:

    attr1 = "mammal"

    attr2 = "dog"

    def fun(self):

        print("I'm a", self.attr1)

        print("I'm a", self.attr2)

Rodger = Dog()

print(Rodger.attr1)

Rodger.fun()

Output: 



mammal
I'm a mammal
I'm a dog

In the above example, an object is created which is basically a dog named Rodger. This class only has two class attributes that tell us that Rodger is a dog and a mammal.
 

Python3

class Dog:

    attr1 = "mammal"

    attr2 = "dog"

    def fun(self):

        print("I'm a", self.attr1)

        print("I'm a", self.attr2)

Rodger = Dog()

print(Rodger.attr1)

Rodger.fun()

The self

  • Class methods must have an extra first parameter in the method definition. We do not give a value for this parameter when we call the method, Python provides it.
  • If we have a method that takes no arguments, then we still have to have one argument.
  • This is similar to this pointer in C++ and this reference in Java.

When we call a method of this object as myobject.method(arg1, arg2), this is automatically converted by Python into MyClass.method(myobject, arg1, arg2) – this is all the special self is about.
 

__init__ method

The __init__ method is similar to constructors in C++ and Java. Constructors are used to initializing the object’s state. Like methods, a constructor also contains a collection of statements(i.e. instructions) that are executed at the time of Object creation. It runs as soon as an object of a class is instantiated. The method is useful to do any initialization you want to do with your object.

Python3

class Person:

    def __init__(self, name):

        self.name = name

    def say_hi(self):

        print('Hello, my name is', self.name)

p = Person('Nikhil')

p.say_hi()

Output: 

Hello, my name is Nikhil