SYBBA-CA(Advance PHP)
SYBBA-CA(Advance PHP)
INTROSPECTION
1.Introduction to Object Oriented
Programming in PHP
What is class ?
Objects with similar characteristics & behaviour are grouped together into a single unit is called class.
1) Class is programmer defined data type, which includes variables & functions.
2) Define class by keyword ‘class’ followed by class name.
3) Class names are case insensitive.
4) Class includes Variables/member property & functions/methods
i) Variables within class are called properties which has name & value.
ii) Functions are called methods which represent actions associated with class.
Class, Property & Methods
Class, Property & Methods
To access methods in terms of a class rather than object, use ‘static’ keyword.
Inside a class, you can refer to static property using the ‘self’ keyword.
Constructor & Destructor
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.
Syntax :
class derived_classname extends base_classname
{
//properties
//methods
}
e.g.
<?php
final class emp { This example shows how to
// some code prevent class overriding
}
We get this output :
// will result in error
class manager extends emp { PHP Fatal error: Class manger may not inherit from final
// some code class (emp) final1.php on line 10
}
?>
Inheritance(Final keyword)
class emp {
final public function show() {
// some code
}
}
This example shows how to
class manager extends emp {
prevent method
// will result in error (function)overriding:
public function intro() {
// some code
}
}
?>
Abstract class
Characteristics of an interfaces:-
Interface are similar to abstract classes. The difference between interfaces and abstract classes are:
1) Interfaces cannot have properties, while abstract classes can
2) All interface methods must be public, while abstract class methods is public or protected
3) All methods in an interface are abstract, so they cannot be implemented in code and the
abstract keyword is not necessary
4) Classes can implement an interface while inheriting from another class at the same time.
5) To implement an interface, a class must use the implements keyword.
Advantages of Interfaces:-
1) It separates implementation & define the structure.
2) It is used to define generic template.
Encapsulation
Encapsulation
The variables/data of a class are hidden from any other class and can be accessed only through any
member function of that class. The data in the class is hidden from the other classes is called as Data
Hiding.
Encapsulation refers to protection of a class’s.
Internal data from the code outside that class & the hiding of details of implementation. Since other
classes don’t have direct access to the properties, you can change the way the method get & set the
properties.
Advantages of Encapsulation :-
1) You can change implementation details at any time without affecting code that uses class.
2) Outside class can’t modify property values of an object built from your class without your knowledge.