DAY 07 | PHP AND MYSQL | VI SEM | B.CA | CLASS AND OBJECTS IN PHP | L1
Summary
TLDRThis video script introduces the fundamental concepts of object-oriented programming in PHP, focusing on classes and objects. It explains that a class serves as a blueprint for creating objects, which are instances with unique states and behaviors. The script demonstrates how to define classes, use constructors, and create objects. It also covers the concept of inheritance, allowing new classes to acquire properties from existing ones, and highlights PHP's support for single inheritance only. The faculty aims to educate students on creating reusable code and enhancing their understanding of PHP's object-oriented features.
Takeaways
- 📘 The script introduces the concept of classes and objects in PHP, emphasizing that a class is a blueprint or template for creating objects, which are instances with their own state and behavior.
- 🔑 The keyword 'class' is used in PHP to define a class, and the class name is user-defined, followed by a constructor method that has the same name as the class.
- 👨🏫 The script is part of a lecture series on object-oriented programming in PHP, delivered by a faculty member from the Department of Computer Science.
- 🏗️ PHP supports object-oriented programming concepts such as classes, objects, polymorphism, data abstraction, and data encapsulation.
- 📝 The script provides an example of creating a class in PHP, including the use of access modifiers like private, public, and protected, and demonstrates how to define properties and methods within a class.
- 🎯 The constructor in a class is a special method that is automatically executed when an object of that class is created, often used for initializing properties.
- 🧬 Inheritance in PHP is discussed as a way to reuse properties from one class in another, with the 'extends' keyword used to create a child class that inherits from a parent class.
- 🔄 PHP supports only single inheritance, meaning a class can inherit from only one parent class, but multiple inheritance can be achieved through multiple single inheritances.
- 📝 An example of single inheritance is given, where a child class acquires the properties and methods of a parent class, allowing access to both the parent's and child's functionalities.
- 📑 The script includes a practical example of creating objects and accessing their properties and methods, demonstrating how to instantiate a class and interact with its members.
- 🔚 The lecture concludes with a teaser for the next session, which will cover form handling in PHP, including different methods supported for form processing.
Q & A
What is the definition of a class in PHP?
-A class in PHP is a blueprint or template that outlines the structure and behavior of an object. It defines a set of properties and methods that objects of that class can have.
How do you create a class in PHP?
-To create a class in PHP, you start with the keyword 'class' followed by the class name and use curly braces to enclose the properties and methods of the class.
What is the purpose of a constructor in a class?
-A constructor in a class is a special method that is automatically executed when an object of that class is created. It is used to initialize the properties of the object.
What is an object in the context of PHP?
-An object in PHP is an instance of a class. It represents a specific entity with its own properties (attributes or variables) and can execute methods (functions or behaviors) defined in the class.
How do you create an object of a class in PHP?
-In PHP, you create an object of a class using the 'new' keyword followed by the class name and calling the constructor with the appropriate parameters if needed.
What is inheritance in PHP?
-Inheritance in PHP is a feature that allows a new class to inherit properties and methods from an existing class. It promotes reusability and reduces code duplication.
What is the difference between a parent class and a child class in PHP?
-In PHP, a parent class (also known as a base class or superclass) is the class from which another class inherits. A child class (also known as a derived class or subclass) is the class that inherits properties and methods from the parent class.
How do you implement single inheritance in PHP?
-In PHP, you implement single inheritance by using the 'extends' keyword to create a new class that extends the properties and methods of an existing class.
Why is inheritance important in object-oriented programming?
-Inheritance is important in object-oriented programming because it allows for code reusability, promotes a hierarchical structure, and enables the creation of specialized classes based on more general classes.
What is the limitation of inheritance in PHP compared to other languages?
-PHP supports only single inheritance, meaning a class can inherit from only one parent class at a time. This is a limitation compared to some other object-oriented languages that support multiple inheritance.
How can you access methods of a parent class from a child class in PHP?
-In PHP, you can access methods of a parent class from a child class by creating an object of the child class and then calling the parent class methods using the object and the appropriate syntax.
Outlines
📚 Introduction to Class and Object in PHP
The speaker introduces the concept of objects, describing them as entities with their own state and behavior. The session will cover class objects and inheritance in PHP. A class in PHP is likened to a blueprint or template that defines the structure and behavior of objects. The speaker explains that PHP is an object-oriented programming language, supporting key concepts like classes, objects, polymorphism, data abstraction, and encapsulation.
🔧 Creating Classes in PHP
This section explains how to create a class in PHP using the 'class' keyword and provides an example of a classroom setup to illustrate the concept. A class is described as a group of objects sharing common properties. The speaker discusses the importance of a class as a template that contains variables and methods. The process of creating an object from a class is explained, emphasizing how an object can access the class's methods and variables.
🔄 Understanding Objects and Instances
The speaker delves into the concept of objects in object-oriented programming, defining an object as an instance of a class, representing a specific entity or concept in the code. Objects have properties (attributes or variables) and can execute methods (functions or behaviors). The example code provided demonstrates how to create an object in PHP using a user-defined class and the 'new' keyword, followed by an explanation of how to access and manipulate object properties.
🔗 Introduction to Inheritance in PHP
This section introduces the concept of inheritance, which allows for the reuse of properties and methods from one class (the parent class) in another class (the child class). The speaker explains the use of the 'extends' keyword in PHP to implement inheritance and illustrates the concept with an example. The limitations of inheritance in PHP are discussed, such as the support for single inheritance only, while multiple inheritance can be achieved using a combination of single inheritances.
🔚 Conclusion and Preview of Next Session
The session concludes with a summary of the topics covered, including the definitions of classes and objects, as well as how to implement inheritance in PHP. The speaker previews the next session, which will focus on form handling in PHP, discussing how to create forms and the different methods available in PHP for handling form data. The speaker thanks the students and encourages them to continue learning.
Mindmap
Keywords
💡Object
💡Class
💡Inheritance
💡Constructor
💡Method
💡Property
💡Instance
💡Access Modifier
💡Polymorphism
💡Encapsulation
Highlights
Definition of object-oriented programming language and its support for concepts like class, object, polymorphism, data abstraction, and data encapsulation.
Explanation of class as a blueprint or template for creating objects.
Description of object as an entity with its own state and behavior.
Introduction to creating a class in PHP using the 'class' keyword.
Discussion on access modifiers: private, public, and protected in PHP classes.
Explanation of a Constructor as a special method in a class with the same name as the class.
Example of creating a class with a parameterized Constructor in PHP.
Demonstration of creating a class with methods to calculate perimeter and area.
Process of creating an object in PHP and accessing its methods and variables.
Introduction to inheritance as a concept for reusability in object-oriented programming.
Differentiation between parent class (super class, base class) and child class (sub class, derived class).
Syntax and implementation of single inheritance in PHP using the 'extends' keyword.
Example of creating a child class that inherits properties from a parent class.
Explanation of how to access methods of both parent and child classes through an object.
Reasoning behind using single inheritance for code reusability in PHP.
Announcement of the next session's topic: form handling in PHP.
Conclusion and thank you note to the students for attending the session.
Transcripts
so what is object object is nothing but
an entity it's going to have its own
state and its own behavior to create a
class in PHP I'm using the keyword class
okay I'm going to use the keyword called
class if I want to inherit the features
from one class to another what I want to
do I want to extend the parent class to
my child
[Music]
class
[Music]
hello to all a warm welcome to my new
unit and the unit name here is class
ired objects in PHP I'm your reati M
faculty from the Department of computer
science vidyashram first grade College
the Temple of Excellence mauru so dear
student in my today's session I'm going
to deal with class objects as well as
inheritance so dear student we all are
familiar with the concept like class and
object what is class and what is object
so dear Student please make a note here
PHP is example for objectoriented
programming language so when I say
objectoriented programming language then
it must support the concepts like class
object polymorphism data abstraction
data encapsulation and so on now if I
consider class what is a class class is
nothing but a blueprint or template and
if I consider object object is nothing
but the instance or functions of the
class or instance of a class okay fine
anyway we will discuss all those things
in depth in my today's session so what
is class class is nothing but a group of
object in simple word I can tell it is a
template okay fine let me to consider
the example here to create a class I'll
start with the keyword class okay fine
now think that okay I'm taking the class
for six same BCA so sixth same B
CA okay fine if I consider the
classroom okay what are the thing I have
I have the board
black board okay fine then I have the
students okay fine then along with
student I have the okay chairs as well
as benches so a class what is class
class is just a template okay class is
just a template or it's a blueprint okay
fine where I'm going to have all the all
the
what variables as well as methods okay
so if I want to access the variables and
methods what I want to do I want to
create a object of the class with the
help of the object I can able to access
the methods and variables of the class
so a class is a group of objects which
have common property so which can have
the common property in simple word it is
a template or it is a blueprint from the
class I can able to create a object so
we can able to create a object so an
entity class is what it's a entity that
can have the state and and behavior okay
fine if I consider object object can
have the state and behavior and it is
known as object so what is object object
is nothing but an entity it's going to
have its own state and its own
behavior okay fine for example if I
consider chair bike marker pen tables
Etc so it's going to have its own state
as well as Behavior okay fine if I
consider this recording room as my class
okay then I can have the objects like
board okay I can have the objects like
pen okay if I consider this pen okay so
the type of pen color of pen width of
pen dimension of pen okay these are
nothing but the okay objects okay fine a
class is a fundamental concepts in PHP
okay it's mainly act as a blueprint or a
template and that outlines the structure
and behavior of the object so who is
going to define the structure and
outline of the object that is none other
than the class so class act as a
foundation for creating the object to
specific type by providing a set of pred
define properties and methods in simple
word what is class class is a blueprint
or it is a template okay so by using
class I can able to create a object
object is nothing but the
entity okay fine now creating a class in
PHP involves the class keyword in the
sense what if I want to create class in
PHP I want to start with the keyword
word called class at the same time what
I want to do I want to specify the user
defined class name a class can also have
a
Constructor in the sense what what is
Constructor Constructor is nothing but
the method okay it's a special method
whose name is same as the class name so
dear student let me to write the class
here to create class I'm going to use
the key word called class and a is
nothing but the class name and if I have
a
method okay same as class name then it
is called as Constructor so class can
have the
Constructor The Constructor of the class
is a special method it's a special
method okay it is automatically executed
when an object is Created from that
class when I create a object of that
class what's going to happen the
constructors are automatically
executed so if I consider this one okay
this is the example for the class how I
can able to create a class in PHP so
I'll start with the PHP okay and I'll
write the keyword called class and here
person is nothing but the class name
what kind of class name it is nothing
but the user defined class name and and
start with the flare bracket okay and I
can have the set of statements and I'm
going to close the flare bracket at the
same time I'm going to close the script
that's nothing but the PHP script okay
fine now see here I have very simple
example here okay so I have the class
called rectangle okay in that okay what
is this private private public that's
nothing but the access modifier so if I
consider access modifier I can have the
three type of access
modifier private public and protected
and see here I have the variable called
length and I have the variable called
width and here I have the function and
in okay and in PHP okay I use a
Constructor okay I use a Constructor
structor and it takes the two parameters
and this is example for your
parameterized Constructor okay fine then
what I will do I'll assign the value to
the length Okay and at the same I'll
assign the value to the width okay fine
now I have one more function called
calculate perimeter okay so it's a
function so I'm going to use the keyword
function to create a function in PHP
what I want to do I want to use a
keyword called function and see this
function okay Returns the value so I'll
write return okay I'm going to find the
perimeter okay fine then what I want to
do once again I have the user defined
method called calculate area it's also
the function user defined function and
I'm going to calculate the area okay
this is the way I can able to create a
class in PHP okay fine now what is an
object till now we discussed how we can
able to create a class to create a class
in PHP I'm using the keyword class okay
I'm going to use the keyword called
class okay fine now what is object
object is nothing nothing but the
instance of an class so it's nothing but
the functionalities of an class so an
object oriented programming language an
object is a instance of the class so
what is instance instance is nothing but
the functionality okay it is nothing but
the
functionality it represents a specific
entity or Concept in our code so object
have its own properties properties in
the sense what that is nothing but the
attributes or variables okay and okay
can execute the methods it's also known
as functions or behavior so object can
have its own property as well as state
so here we discuss how to create an
object see here I have the dollar sign
okay variable so it is nothing but your
object name it is nothing but your
object name user defined object name
equal to new is a keyword okay and class
name so class name is nothing but your
okay see I have class name opening and
closing parenthesis what it indicates it
indicates it's a Constructor okay fine
now I have the small example code here
see we know that we are going to embed
PHP with the HTML so I'll start start
with HTML okay I'll start with the body
okay inside the body I'm going to create
a class what is my class name it's
nothing but the fruits okay fine now see
I have the variable called name and it's
of type public
so Apple okay now what I'm doing here
I'm creating the creating the object now
okay I'm creating the object Apple okay
how new it is your class name what's my
class name fruit what kind of
Constructor it is default Constructor so
it is default Constructor or no
parameterized
Constructor now Apple okay with the help
of object okay using Arrow operator I'm
going to call the name okay and I'm
going to assign the string called Apple
I'm assigning Apple now if I want to
display uh the value apple or string
Apple what I will do I'll go with Echo
okay then what is this it is my object
Arrow operator and my variable name
called name then what I will do I'll
close the PHP script okay I'll close the
body and I'll close the HTML and what is
my output apple is my output okay okay
fine now I have the concept called
inheritance so what is inheritance
inheritance is nothing but the
reusability reusability in the sense
what when I want to reuse the property
what I will do I'll go with the
inheritance so whenever I want to
understand the concept like inheritance
you have to remember two things one is
your old class one is your new class
okay when I when the new class okay when
I create a new class using the old class
then it is called inheritance so I'm
going to derive a new class from the old
class that is known as
inheritance and in C++ Java or in our
net we have discussed inheritance there
we have discussed single inheritance
multiple inheritance multi-level
inheritance and hierarchical inheritance
but in PHP we can have only single
inheritance so it is a concept of
accessing the features of one class from
another class so it's a feature of
accessing from one class to another
class think that okay I have some
features in my old class okay and I want
to access that feature in my new class
that time what I will do I'll go with
the inheritance so if we inherit the
class features into another class we can
access both the class property so this
old class is called by the name super
class okay or it is called by the name
parent class or it is called by the name
Base Class and what about new class this
new class is nothing but the child class
okay okay or it's nothing but the sub
class or it is nothing but the derived
class okay it's nothing but the derived
class so if I want to inherit the
features from one class to another what
I want to do I want to extend the parent
class to my child class so what I will
do I'll go with the extend keyword okay
so I'll go with the extend key
and in PHP we can able to have only
single
inheritance even I can have multiple
inheritance that can be achieved by
using multiple single
inheritance so to implement inheritance
in PHP what I will do I'll go with the
extend keyword okay fine now let's
discuss the syntax here I have the class
called derived okay okay then extends
Base Class okay fine let me to draw the
single inheritance first see I have
the class A what is this it's my parent
class or super class or it is nothing
but the Base Class okay now I have the
child called b b can able to acquire the
property of a okay so it is nothing but
the sub class or child class or derived
class okay fine now first what I want to
do I want to create a class called a
okay it can have its own property okay
fine now okay I'm going to create a
class called B and this B extends
the property of a okay now we can have
all the properties of a in the sense
what all the Bel belongings of a now see
here class the child class is nothing
but the derived class okay I'll start
with the extend keyword and base class
is nothing but your parent class now I
can have the members and functions here
okay fine now okay so I'll start with
the this is just a syntax now now okay I
want to create a single inheritance so
where I create the single inheritance in
my PHP script so I'll start with the PHP
so what's my class name a is nothing but
the class name okay in this class I have
a function called F1 so what's my
function name F1 and I'm going to
display classes and objects so I'm just
going to convey the message that it is
just a class and object objects okay
fine now same thing I want to have in my
next class so what I will do I'm going
to extend the class so B is my child
class okay B is my child Class A is my
parent class I want to acquire the
properties of a in my B so Class B
extends a in this I have a function
called F2 then what about this function
it's a keyword used to create a function
in PHP and I'm going to have the message
called
PHP okay fine now what I will do okay
I'm going to create a object only for my
child class by using the object of child
class I can able to access the methods
and okay methods and variables of the
child class as well as the parent class
so with the help of object I'm going to
call a method of the parent class and
with the help of object I'm going to
call the method of child class so F1
method present in your parent class and
F2 method is present in your that is fun
two is present in your child class so
this is the way I can able to implement
single in inheritance so why I use
single inheritance it is mainly to reuse
the property so instead of writing same
code again and again what I can able to
do I can able to reuse the properties
for that I'm going to have the
inheritance okay fine and PHP supports
only single inheritance even I can have
the multiple inheritance by using single
inheritance anyway dear student in my
today's class I started with what is
class what is object and how I can able
to create a inheritance in my next class
I'm going to deal with okay I'm going to
deal with form handling in the sense how
I can able to create a forms in PHP what
are the different methods supported by
the PHP to handle the forms all those
things we will discuss in my next
session anyway dear student thank you
for watching my session and let's meet
in the next session with more
information about PHP until that keep
watching and keep learning thank you
Ver Más Videos Relacionados
5.0 / 5 (0 votes)