0% found this document useful (0 votes)
49 views19 pages

chp1 semII

Classes and objects in object-oriented programming allow programmers to define custom data types that bundle together code (methods) and data (properties). A class acts as a template for creating many objects that share the same characteristics. Objects are individual instances of a class. Member variables store data within each object, while member functions provide methods to access or modify that data. Encapsulation refers to restricting access to some methods and properties within a class.

Uploaded by

sakshisunilaru08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views19 pages

chp1 semII

Classes and objects in object-oriented programming allow programmers to define custom data types that bundle together code (methods) and data (properties). A class acts as a template for creating many objects that share the same characteristics. Objects are individual instances of a class. Member variables store data within each object, while member functions provide methods to access or modify that data. Encapsulation refers to restricting access to some methods and properties within a class.

Uploaded by

sakshisunilaru08
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 19

Classes and Object

• Class − This is a programmer-defined data type, which


includes local functions as well as local data. You can think of
a class as a template for making many instances of the same
kind (or class) of object.

• Object − An individual instance of the data structure defined


by a class. You define a class once and then make many
objects that belong to it. Objects are also known as instance.
• Member Variable − These are the variables defined inside a
class. This data will be invisible to the outside of the class and
can be accessed via member functions. These variables are
called attribute of the object once an object is created.

• Member function − These are the function defined inside a


class and are used to access object data.
• <?php
• class bca
• {
• // member variable
• public $var = 'a default value';

• // member function
• public function displayVar()
• {
• echo $this->var;
• }
• }
• $hello= new bca; //hello is object of class bca
• $hello-> displayVar();
• ?>
• To create an object of a given class use the new keyword

• With a method, the $this variable contains a reference to the


object on which the method was called.

• With a class definition you can specify which methods and


properties are publicly accessible and which are accessible
only from within the class using public and private access
modifiers. You can use this to provide Encapsulation
Inheritance

Inheritance in OOP = When a class derives from another class.

The child class will inherit all the public and protected properties
and methods from the parent class.

In addition, it can have its own properties and methods.

An inherited class is defined by using the extends keyword.


• If a derived class has a property or method with the same
name as one in its parent class, the property or method in the
derived class takes precedence over the property or method in
the parent class.

• To access overridden method, use the parent :: method()


• parent:: birthday(); i.e. call parent class’s birthday method

• If a method might be sub classed and you want to ensure that


you are calling it on the current class, use self:: method()
• self::birthday(); i.e. call this class’s birthday method
Introspection
• introspection is the ability of a program to examine an object's
characteristics, such as its name, parent class (if any),
properties, and methods.

• With introspection, you can write code that operates on any


class or object.

• You don't need to know which methods or properties are


defined when you write your code; instead, you can discover
that information at runtime, which makes it possible for you to
write generic debuggers, serializers, profilers etc.
Introspection Functions provided by PHP

• To determine whether a class exists, use the class_exists( )


function, which takes in a string and returns a Boolean value.

• $yes_no = class_exists(classname);

• Alternately, you can use the get_declared_classes( ) function,


which returns an array of defined classes and checks if the
class name is in the returned array:
• $classes = get_declared_classes( );
• You can get the methods and properties that exist in a class (including
those that are inherited from superclasses) using the get_class_methods( )
and get_class_vars( ) functions.
• These functions take a class name and return an array:

• $methods = get_class_methods(classname);
• $properties = get_class_vars(classname);

• The class name can be a bare word, a quoted string, or a variable


containing the class name:
• $class = 'Person';
• $methods = get_class_methods($class);
• $methods = get_class_methods(Person); // same
• $methods = get_class_methods('Person'); // same
• Use get_parent_class( ) to find a class's parent class:

• $superclass = get_parent_class(classname);
• Cal1.html
• Intro1.php
Object functions
• To get the class to which an object belongs, first make sure it is an object
using the is_object( ) function, then get the class with the get_class( )
function:
• $yes_no = is_object(var);
• $classname = get_class(object);

• Before calling a method on an object, you can ensure that it exists using
the method_exists( ) function:
• $yes_no = method_exists(object, method);
• Just as get_class_vars( ) returns an array of properties for a
class, get_object_vars( ) returns an array of properties set in an
object:

• $array = get_object_vars(object);

• just as get_class_vars( ) returns only those properties with


default values, get_object_vars( ) returns only those properties
that are set:
• Example
• class Person
• {
• var $name;
• var $age;
• }
• $fred = new Person;
• $fred->name = 'Fred';
• $props = get_object_vars($fred); // $props is array('name' =>
'Fred');
Serialization
• Arrays certainly are useful.
• Sometimes, you’ll run into cases where it would be useful to
store an entire array in a field in a database.
• It’s not something you want to do if you can avoid it, by
structuring your database’s tables more efficiently for
example, but there are cases where it’s unavoidable.
• That’s where serialize() comes in. You can pass an array to the
function, and it will return a string that is essentially the array
flattened and mashed down.
• You can then unserialize() it to obtain the full array once
again.
• <?php
$the_array = array( "Lorem", "Ipsum", "Dolor" );
$serialized = serialize($the_array);
print $serialized;
?>

• This will output a:3:


{i:0;s:5:"Lorem";i:1;s:5:"Ipsum";i:2;s:5:"Dolor";}It’s
the whole array in string form, suitable for insertion
into a database field.
• When serializing objects, PHP will attempt to call the member
function __sleep() prior to serialization. This is to allow the
object to do any last minute clean-up, etc. prior to being
serialized.

• Likewise, when the object is restored using unserialize() the


__wakeup() member function is called.
Encapsulation
• The ability to hide the details of implementation is known as
encapsulation.
• The encapsulation is used for protection of a class’s internal
data from outside that class and hiding the details of
implementation.
• encapsulation means, visibility. It refers to visibility of
methods or properties of a class.
• In general, how would you like to make visual of your variable
or function in procedural coding.
• 3 key words like public, private and protected we use to
decide that whether our methods or properties are going to
use child class or within class.
• Public – The method is publicly available and can be accessed
by all subclasses.
• Protected – the method / function / property is available to
the parent class and all inheriting classes or we call them
subclasses or child classes.
• Private – the method is private and only available to the
parent class / base class.

You might also like