DAY 07 | PHP AND MYSQL | VI SEM | B.CA | CLASS AND OBJECTS IN PHP | L1

Vidhyaashram
4 Mar 202420:43

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

00:00

📚 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.

05:04

🔧 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.

10:07

🔄 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.

15:07

🔗 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.

20:09

🔚 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

In the context of the video, an 'Object' refers to an entity with its own state and behavior. It is a fundamental concept in object-oriented programming (OOP), representing a specific instance of a class. For example, in the script, a 'pen' is used to illustrate an object, with attributes like type, color, and width defining its state.

💡Class

A 'Class' is defined as a blueprint or template for creating objects in OOP. It outlines the structure and behavior of the objects, including their properties and methods. In the script, the class is used to represent a concept such as a 'classroom', which includes elements like a 'blackboard', 'students', 'chairs', and 'benches'.

💡Inheritance

Inheritance is a core concept in OOP that allows a new class to acquire the properties of an existing class. It is about reusability and is used to avoid code duplication. In the script, inheritance is discussed in the context of extending features from a 'parent class' to a 'child class', such as creating a 'Rectangle' class that inherits properties from a 'Shape' class.

💡Constructor

A 'Constructor' is a special method in a class that is automatically executed when an object of that class is created. It is used to initialize the object's state. The script provides an example of a constructor in the 'Rectangle' class, which takes parameters to set the 'length' and 'width' of the rectangle upon creation.

💡Method

A 'Method' in OOP is a function that is associated with an object and defines the behavior of the object. In the script, methods are used to perform actions like calculating the 'perimeter' and 'area' of a rectangle, demonstrating how objects can execute behaviors defined by their class.

💡Property

A 'Property' in the context of OOP refers to the attributes of an object, representing its state. The script mentions properties in the context of a class, such as the 'length' and 'width' of a rectangle, which are properties that define the state of the object.

💡Instance

An 'Instance' is a specific occurrence of an object created from a class. It represents a single entity with its own set of properties and methods. The script explains that an object is an instance of a class, such as a 'pen' being an instance of a 'WritingInstrument' class.

💡Access Modifier

Access modifiers in OOP are used to control the accessibility of class members, such as variables and methods. The script mentions 'private', 'public', and 'protected' as types of access modifiers, which determine where and how class members can be accessed, like making the 'length' and 'width' of a rectangle private to restrict access from outside the class.

💡Polymorphism

Although not explicitly mentioned in the script, 'Polymorphism' is a concept in OOP that allows objects of different classes to be treated as objects of a common superclass. It is related to the video's theme as it is one of the fundamental concepts of OOP alongside classes, objects, and inheritance.

💡Encapsulation

Encapsulation is the practice of bundling the data (attributes) and methods that operate on the data into a single unit or class. It is also not directly mentioned in the script but is a key principle of OOP. It is related to the script's discussion of classes as templates that encapsulate the state and behavior of objects.

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

play00:00

so what is object object is nothing but

play00:02

an entity it's going to have its own

play00:05

state and its own behavior to create a

play00:09

class in PHP I'm using the keyword class

play00:13

okay I'm going to use the keyword called

play00:15

class if I want to inherit the features

play00:19

from one class to another what I want to

play00:22

do I want to extend the parent class to

play00:25

my child

play00:26

[Music]

play00:28

class

play00:30

[Music]

play00:32

hello to all a warm welcome to my new

play00:35

unit and the unit name here is class

play00:38

ired objects in PHP I'm your reati M

play00:42

faculty from the Department of computer

play00:44

science vidyashram first grade College

play00:47

the Temple of Excellence mauru so dear

play00:50

student in my today's session I'm going

play00:53

to deal with class objects as well as

play00:57

inheritance so dear student we all are

play01:00

familiar with the concept like class and

play01:04

object what is class and what is object

play01:08

so dear Student please make a note here

play01:11

PHP is example for objectoriented

play01:15

programming language so when I say

play01:18

objectoriented programming language then

play01:22

it must support the concepts like class

play01:26

object polymorphism data abstraction

play01:29

data encapsulation and so on now if I

play01:34

consider class what is a class class is

play01:37

nothing but a blueprint or template and

play01:42

if I consider object object is nothing

play01:45

but the instance or functions of the

play01:49

class or instance of a class okay fine

play01:52

anyway we will discuss all those things

play01:55

in depth in my today's session so what

play01:58

is class class is nothing but a group of

play02:03

object in simple word I can tell it is a

play02:07

template okay fine let me to consider

play02:10

the example here to create a class I'll

play02:14

start with the keyword class okay fine

play02:17

now think that okay I'm taking the class

play02:22

for six same BCA so sixth same B

play02:31

CA okay fine if I consider the

play02:34

classroom okay what are the thing I have

play02:37

I have the board

play02:41

black board okay fine then I have the

play02:46

students okay fine then along with

play02:50

student I have the okay chairs as well

play02:54

as benches so a class what is class

play02:58

class is just a template okay class is

play03:02

just a template or it's a blueprint okay

play03:06

fine where I'm going to have all the all

play03:10

the

play03:11

what variables as well as methods okay

play03:15

so if I want to access the variables and

play03:18

methods what I want to do I want to

play03:20

create a object of the class with the

play03:23

help of the object I can able to access

play03:26

the methods and variables of the class

play03:30

so a class is a group of objects which

play03:35

have common property so which can have

play03:38

the common property in simple word it is

play03:42

a template or it is a blueprint from the

play03:46

class I can able to create a object so

play03:50

we can able to create a object so an

play03:54

entity class is what it's a entity that

play03:57

can have the state and and behavior okay

play04:01

fine if I consider object object can

play04:03

have the state and behavior and it is

play04:07

known as object so what is object object

play04:11

is nothing but an entity it's going to

play04:14

have its own state and its own

play04:18

behavior okay fine for example if I

play04:20

consider chair bike marker pen tables

play04:26

Etc so it's going to have its own state

play04:29

as well as Behavior okay fine if I

play04:32

consider this recording room as my class

play04:35

okay then I can have the objects like

play04:38

board okay I can have the objects like

play04:41

pen okay if I consider this pen okay so

play04:45

the type of pen color of pen width of

play04:48

pen dimension of pen okay these are

play04:51

nothing but the okay objects okay fine a

play04:54

class is a fundamental concepts in PHP

play04:59

okay it's mainly act as a blueprint or a

play05:03

template and that outlines the structure

play05:07

and behavior of the object so who is

play05:10

going to define the structure and

play05:12

outline of the object that is none other

play05:16

than the class so class act as a

play05:20

foundation for creating the object to

play05:25

specific type by providing a set of pred

play05:29

define properties and methods in simple

play05:32

word what is class class is a blueprint

play05:36

or it is a template okay so by using

play05:40

class I can able to create a object

play05:43

object is nothing but the

play05:45

entity okay fine now creating a class in

play05:49

PHP involves the class keyword in the

play05:53

sense what if I want to create class in

play05:56

PHP I want to start with the keyword

play05:59

word called class at the same time what

play06:02

I want to do I want to specify the user

play06:05

defined class name a class can also have

play06:09

a

play06:11

Constructor in the sense what what is

play06:13

Constructor Constructor is nothing but

play06:15

the method okay it's a special method

play06:18

whose name is same as the class name so

play06:21

dear student let me to write the class

play06:24

here to create class I'm going to use

play06:28

the key word called class and a is

play06:32

nothing but the class name and if I have

play06:35

a

play06:36

method okay same as class name then it

play06:39

is called as Constructor so class can

play06:42

have the

play06:44

Constructor The Constructor of the class

play06:47

is a special method it's a special

play06:49

method okay it is automatically executed

play06:53

when an object is Created from that

play06:56

class when I create a object of that

play07:00

class what's going to happen the

play07:02

constructors are automatically

play07:05

executed so if I consider this one okay

play07:08

this is the example for the class how I

play07:12

can able to create a class in PHP so

play07:15

I'll start with the PHP okay and I'll

play07:18

write the keyword called class and here

play07:22

person is nothing but the class name

play07:24

what kind of class name it is nothing

play07:26

but the user defined class name and and

play07:29

start with the flare bracket okay and I

play07:32

can have the set of statements and I'm

play07:35

going to close the flare bracket at the

play07:37

same time I'm going to close the script

play07:39

that's nothing but the PHP script okay

play07:42

fine now see here I have very simple

play07:44

example here okay so I have the class

play07:50

called rectangle okay in that okay what

play07:53

is this private private public that's

play07:56

nothing but the access modifier so if I

play07:59

consider access modifier I can have the

play08:01

three type of access

play08:03

modifier private public and protected

play08:07

and see here I have the variable called

play08:12

length and I have the variable called

play08:16

width and here I have the function and

play08:20

in okay and in PHP okay I use a

play08:26

Constructor okay I use a Constructor

play08:29

structor and it takes the two parameters

play08:33

and this is example for your

play08:35

parameterized Constructor okay fine then

play08:38

what I will do I'll assign the value to

play08:40

the length Okay and at the same I'll

play08:43

assign the value to the width okay fine

play08:47

now I have one more function called

play08:51

calculate perimeter okay so it's a

play08:55

function so I'm going to use the keyword

play08:58

function to create a function in PHP

play09:01

what I want to do I want to use a

play09:03

keyword called function and see this

play09:07

function okay Returns the value so I'll

play09:11

write return okay I'm going to find the

play09:15

perimeter okay fine then what I want to

play09:18

do once again I have the user defined

play09:22

method called calculate area it's also

play09:26

the function user defined function and

play09:29

I'm going to calculate the area okay

play09:33

this is the way I can able to create a

play09:36

class in PHP okay fine now what is an

play09:41

object till now we discussed how we can

play09:44

able to create a class to create a class

play09:48

in PHP I'm using the keyword class okay

play09:52

I'm going to use the keyword called

play09:54

class okay fine now what is object

play09:58

object is nothing nothing but the

play09:59

instance of an class so it's nothing but

play10:02

the functionalities of an class so an

play10:06

object oriented programming language an

play10:09

object is a instance of the class so

play10:13

what is instance instance is nothing but

play10:15

the functionality okay it is nothing but

play10:18

the

play10:19

functionality it represents a specific

play10:23

entity or Concept in our code so object

play10:28

have its own properties properties in

play10:32

the sense what that is nothing but the

play10:34

attributes or variables okay and okay

play10:39

can execute the methods it's also known

play10:42

as functions or behavior so object can

play10:45

have its own property as well as state

play10:49

so here we discuss how to create an

play10:53

object see here I have the dollar sign

play10:57

okay variable so it is nothing but your

play11:00

object name it is nothing but your

play11:01

object name user defined object name

play11:04

equal to new is a keyword okay and class

play11:08

name so class name is nothing but your

play11:10

okay see I have class name opening and

play11:12

closing parenthesis what it indicates it

play11:15

indicates it's a Constructor okay fine

play11:19

now I have the small example code here

play11:22

see we know that we are going to embed

play11:26

PHP with the HTML so I'll start start

play11:29

with HTML okay I'll start with the body

play11:32

okay inside the body I'm going to create

play11:35

a class what is my class name it's

play11:37

nothing but the fruits okay fine now see

play11:41

I have the variable called name and it's

play11:44

of type public

play11:46

so Apple okay now what I'm doing here

play11:50

I'm creating the creating the object now

play11:53

okay I'm creating the object Apple okay

play11:57

how new it is your class name what's my

play12:01

class name fruit what kind of

play12:03

Constructor it is default Constructor so

play12:06

it is default Constructor or no

play12:10

parameterized

play12:11

Constructor now Apple okay with the help

play12:14

of object okay using Arrow operator I'm

play12:19

going to call the name okay and I'm

play12:23

going to assign the string called Apple

play12:26

I'm assigning Apple now if I want to

play12:30

display uh the value apple or string

play12:33

Apple what I will do I'll go with Echo

play12:37

okay then what is this it is my object

play12:41

Arrow operator and my variable name

play12:45

called name then what I will do I'll

play12:48

close the PHP script okay I'll close the

play12:51

body and I'll close the HTML and what is

play12:55

my output apple is my output okay okay

play12:59

fine now I have the concept called

play13:02

inheritance so what is inheritance

play13:04

inheritance is nothing but the

play13:07

reusability reusability in the sense

play13:09

what when I want to reuse the property

play13:12

what I will do I'll go with the

play13:15

inheritance so whenever I want to

play13:18

understand the concept like inheritance

play13:20

you have to remember two things one is

play13:23

your old class one is your new class

play13:27

okay when I when the new class okay when

play13:30

I create a new class using the old class

play13:34

then it is called inheritance so I'm

play13:37

going to derive a new class from the old

play13:42

class that is known as

play13:44

inheritance and in C++ Java or in our

play13:51

net we have discussed inheritance there

play13:54

we have discussed single inheritance

play13:57

multiple inheritance multi-level

play14:00

inheritance and hierarchical inheritance

play14:04

but in PHP we can have only single

play14:08

inheritance so it is a concept of

play14:11

accessing the features of one class from

play14:14

another class so it's a feature of

play14:17

accessing from one class to another

play14:19

class think that okay I have some

play14:22

features in my old class okay and I want

play14:25

to access that feature in my new class

play14:28

that time what I will do I'll go with

play14:30

the inheritance so if we inherit the

play14:34

class features into another class we can

play14:37

access both the class property so this

play14:40

old class is called by the name super

play14:44

class okay or it is called by the name

play14:47

parent class or it is called by the name

play14:50

Base Class and what about new class this

play14:55

new class is nothing but the child class

play14:58

okay okay or it's nothing but the sub

play15:02

class or it is nothing but the derived

play15:07

class okay it's nothing but the derived

play15:09

class so if I want to inherit the

play15:13

features from one class to another what

play15:16

I want to do I want to extend the parent

play15:19

class to my child class so what I will

play15:22

do I'll go with the extend keyword okay

play15:26

so I'll go with the extend key

play15:29

and in PHP we can able to have only

play15:33

single

play15:34

inheritance even I can have multiple

play15:38

inheritance that can be achieved by

play15:40

using multiple single

play15:43

inheritance so to implement inheritance

play15:47

in PHP what I will do I'll go with the

play15:50

extend keyword okay fine now let's

play15:53

discuss the syntax here I have the class

play15:57

called derived okay okay then extends

play16:01

Base Class okay fine let me to draw the

play16:05

single inheritance first see I have

play16:09

the class A what is this it's my parent

play16:13

class or super class or it is nothing

play16:16

but the Base Class okay now I have the

play16:19

child called b b can able to acquire the

play16:23

property of a okay so it is nothing but

play16:27

the sub class or child class or derived

play16:31

class okay fine now first what I want to

play16:35

do I want to create a class called a

play16:39

okay it can have its own property okay

play16:42

fine now okay I'm going to create a

play16:44

class called B and this B extends

play16:49

the property of a okay now we can have

play16:55

all the properties of a in the sense

play16:57

what all the Bel belongings of a now see

play17:02

here class the child class is nothing

play17:06

but the derived class okay I'll start

play17:10

with the extend keyword and base class

play17:13

is nothing but your parent class now I

play17:16

can have the members and functions here

play17:21

okay fine now okay so I'll start with

play17:25

the this is just a syntax now now okay I

play17:29

want to create a single inheritance so

play17:33

where I create the single inheritance in

play17:36

my PHP script so I'll start with the PHP

play17:40

so what's my class name a is nothing but

play17:42

the class name okay in this class I have

play17:46

a function called F1 so what's my

play17:49

function name F1 and I'm going to

play17:51

display classes and objects so I'm just

play17:54

going to convey the message that it is

play17:56

just a class and object objects okay

play17:59

fine now same thing I want to have in my

play18:03

next class so what I will do I'm going

play18:06

to extend the class so B is my child

play18:10

class okay B is my child Class A is my

play18:13

parent class I want to acquire the

play18:16

properties of a in my B so Class B

play18:20

extends a in this I have a function

play18:23

called F2 then what about this function

play18:25

it's a keyword used to create a function

play18:29

in PHP and I'm going to have the message

play18:32

called

play18:34

PHP okay fine now what I will do okay

play18:37

I'm going to create a object only for my

play18:41

child class by using the object of child

play18:45

class I can able to access the methods

play18:48

and okay methods and variables of the

play18:52

child class as well as the parent class

play18:56

so with the help of object I'm going to

play18:59

call a method of the parent class and

play19:03

with the help of object I'm going to

play19:06

call the method of child class so F1

play19:11

method present in your parent class and

play19:16

F2 method is present in your that is fun

play19:19

two is present in your child class so

play19:23

this is the way I can able to implement

play19:27

single in inheritance so why I use

play19:30

single inheritance it is mainly to reuse

play19:35

the property so instead of writing same

play19:37

code again and again what I can able to

play19:40

do I can able to reuse the properties

play19:43

for that I'm going to have the

play19:46

inheritance okay fine and PHP supports

play19:49

only single inheritance even I can have

play19:52

the multiple inheritance by using single

play19:56

inheritance anyway dear student in my

play19:58

today's class I started with what is

play20:01

class what is object and how I can able

play20:05

to create a inheritance in my next class

play20:08

I'm going to deal with okay I'm going to

play20:11

deal with form handling in the sense how

play20:15

I can able to create a forms in PHP what

play20:19

are the different methods supported by

play20:22

the PHP to handle the forms all those

play20:26

things we will discuss in my next

play20:28

session anyway dear student thank you

play20:31

for watching my session and let's meet

play20:34

in the next session with more

play20:37

information about PHP until that keep

play20:40

watching and keep learning thank you

Rate This

5.0 / 5 (0 votes)

Etiquetas Relacionadas
PHPOOPClassesObjectsInheritanceProgrammingConstructorMethodsTemplatesBlueprintsInstance
¿Necesitas un resumen en inglés?