ENGLISH 7_Q4_LESSON 2_ Employing a Variety of Strategies for Effective Interp...
Only oop
1.
2. Object oriented programming concept use to make
powerful, robust and secure programming instruction.
with any language reference there are
three very important object oriented programming
concepts.
1. Encapsulation and Data Abstraction.
2. Inheritance(Multiple and Multilevel).
3. Polymorphism.
3. Real world programming.
ability to simulate real-world event much more effectively
Reusable of code.
Information hiding.
Better able to create GUI alications
programmers are able to reach their goals faster
Programmers are able to produce faster, more accurate
4. Class is a blueprint of related data members and
methods.
Classes are the fundamental construct behind object
oriented programming.
It is a self contained, independent collection of
variables and functions, which work together to
perform one or more specific or related task.
Variables within a class are called properties and
functions are called methods.
5. Class always start with class name. After this we have to
write class name without parentheses. Inside curly braces
we have to define all programming logic. Eg:--
class class_name
{
-----------
-------------
}
6. Object is the instance of the class .
Once a class has been defined, objects can be created from the
class with the new keyword.
Class methods and properties can directly be accessed through
this object instance.
Dot operator(->):- (->)symbol used to connect objects to their
properties or methods.
[NOTES:-We can’t access any property of the class without their object.]
7. $VAR = new classname();
new keyword is responsible to initialize a
object of the class.
8. We call properties to the variables inside a class.
Properties can accept values like strings, integers, and
booleans (true/false values), like any other variable.
Property could be public, private , protected
class Car {
public $comp;
public $color = 'beige';
public $hasSunRoof = true;
}
9. Methods are actions that can be performed
on objects. Object properties can be both
primitive values, other objects, and functions.
An object method is an object property
containing a function definition.
Property could be public, private , protected
10. <?php
class a
{ function add($a,$b)
{ $add=$a+$b;
echo "This is function add:--".$add;
}
function sub($x,$y)
{ $sub=$x-$y;
echo "This is funcion sub--".$sub;
}
}
$obj = new a;
$obj->add(10,20);
$obj->sub(50,20);
?>
11. There are three levels of visibility exist,
ranging from most visible to least visible.
◦ Public
◦ Protected
◦ private
12. Controlling the Visibility of Properties and Methods
When working with classes, you can even restrict access to its
properties and methods using the visibility keywords for greater
control. There are three visibility keywords
•public — A public property or method can be accessed
anywhere, from within the class and outside. This is the default
visibility for all class members in PHP.
•protected — A protected property or method can only be
accessed from within the class
•ss itself or in child or inherited classes i.e. classes that extends
that class.
•private — A private property or method is accessible only from
within the class that defines it. Even child or inherited classes
cannot access private properties or methods.
13. <?php
class Example {
public $name="BCA II";
private $age;
}
$example = new Example;
echo $example -> name . "n";
echo $example -> age;
?>
14. Constructor automatically call when object
will be initialize/found.
PHP introduce a new functionality to define a
constructor i.e. __construct().
By using this function we can define a
constructor and it is known as predefined
constructor.
15. <?php
class A
{
function testA() //function
{
echo "n This is test A";
}
function __construct() //constructor
{
echo "This is predefined constructor of class A";
}
//function A ()
//{ echo "This is constructor a";
// }
}
$obj = new A;
$obj->testA();
?>
17. <?php
class A
{
function __construct()
{
echo "hello constuctor.....n ";
}
function __destruct() //Destructor
{
echo "ending up........stay at home";
}
}
$obj = new A();
unset($obj);
?>
18. Constants are pieces of information stored in the
computer's memory which cannot be changed once
assigned.
Class Constants are constants that are declared
within classes.
The const keyword is used to declare a class
constant.
The class constant should be declared inside the class
definition. No $ (dollar sign) used.
<?php
class Welcome
{
const GREET = 'Hello World';
}
19. Inside the class:
The self keyword and Scope Resolution
Operator (::) is used to access class constants
from the methods in a class.
public function greet()
{ echo self::GREET;
}
20. Outside the class:
The class name and constant name is used to
access a class constant from outside a class.
echo Welcome::GREET;
21. <?php
class Welcome
{
const GREET = 'Hello World';
public function greet()
{ echo self::GREET;
}
}
$obj = new Welcome();
$obj -> greet();
echo "n";
echo Welcome::GREET;
echo $obj->GREET;
?>
22. Static Methods and Properties can be
accessed without instantiating. (Without
creating objects)
Static methods can be called directly without
needing an instance of the class (an object).
And, $this pseudo-variable is not available
inside static methods.
The static keyword is used to declare static
methods.
24. Static methods can be accessed from outside
the class using the class name and Scope
Resolution Operator (::). (Only if the visibility
is public)
MyClass::myStaticMethod();
25. <?php
class myexample
{
public static function getDomain()
{ return "Developer";
}
}
echo myexample::getDomain();
//$obj = new myexample;
//$obj->getDomain();
?>
26. Static methods can be called from methods of
other classes in the same way. To do this
visibility of the static method should be
public.
27. <?php
class MyClass
{
public static function myStaticMethod()
{ echo "Hello";
}
}
class OtherClass
{ public function myMethod()
{MyClass::myStaticMethod();
}
}
$obj = new Otherclass;
$obj->myMethod();
?>
28. $this can be used inside any method to refer
to the current object.
$this is always a reference to the object on
which the method was invoked.
29. By using inheritance concept we can inherit other class
properties with any specific class.
In this process we have to establish relation between both
class using extends keyword.
By using object of child class we can inherit parent class
properties.
It has two types.
◦ Multilevel inheritance
◦ Multiple inheritance (Not available in PHP)
[multiple inheritance feature is only available in c++ prog.
language And multilevel inheritance feature is available in
programming languages like PHP, Java etc.]21
30. Multiple Inheritance : we can inherit more
than one class in the same class.
Multi-Level Inheritance: where one class can
inherit only one base class and the derived
class can become base class of some other
class.
31. In Multiple inheritance like this:
Base Class
_________________
/ |
Class1 Class2 Class3
Multilevel Inheritance:
Base Class
||
Class1
||
Class2
32. get_class():- this function returns the name of
the class from which an object was
instantiated.
get_parent_class():- it returns the name of its
parent.
parent():- keyword parent provides an easy
shortcut to refer to the current class parent.
33. <?php
class a{
public $age;
function __construct(){
echo "creating a new
".get_class($this)."--- class";
}
function setAge($age){
$this->age=$age;
}
function getAge(){
return $this->age;
}
}
class b extends a
{
public $name;
function __construct(){
parent::__construct();
}
function setName($name){
$this->name = $name;
}
function getName(){
return $this->name;
}
}
$obj = new b;
$obj->setAge(25);
$obj->setName("Kamlesh Dutt");
echo $obj->getName().'is now
'.$obj->getAge().' years old';
?>
34. An abstract class is a incomplete class which has
abstract methods. The abstract methods are
those which are only declared but not defined.
The abstract class is always inherited and this is
the responsibility of sub class to define all the
abstract method of its super class. If the sub
class does not define any single method of
abstract class, then it is also treated as abstract
class.
It is not allowed to create an instance of a class
that has been defined as abstract.
35. Abstract classes allow you to provide default
functionality for the subclasses. Common
knowledge at this point. Why is this
extremely important though? If you plan on
updating this base class throughout the life
of your program, it is best to allow that base
class to be an abstract class. Why? Because
you can make a change to it and all of the
inheriting classes will now have this new
functionality.
36. If the base class will be changing often and
an interface was used instead of an abstract
class, we are going to run into problems.
Once an interface is changed, any class that
implements that will be broken.
37. <?php
abstract class AbstractClass
{
// Force Extending class to
define this method
abstract protected function
getValue();
abstract protected function
prefixValue($prefix);
// Common method
public function printOut()
{
print $this->getValue() .
"n";
}
}
class ConcreteClass1 extends
AbstractClass
{
protected function getValue() {
return “Parent Class”;
}
public function
prefixValue($prefix) {
return "{$prefix}calling parent";
}
}
$class1 = new ConcreteClass1;
$class1->printOut();
echo $class1->prefixValue(‘Child') .
“n";
?>
//Parent Class
Child calling parent
38. Object interfaces allow you to create code
which specifies which methods a class must
implement, without having to define how
these methods are handled.
Interfaces are defined using the interface
keyword, in the same way as a standard class,
but without any of the methods having their
contents defined.
All methods declared in an interface must be
public, this is the nature of an interface.
39. implements
To implement an interface, the implements
operator is used. All methods in the interface
must be implemented within a class; failure to do
so will result in a fatal error. Classes may
implement more than one interface if desired by
separating each interface with a comma.
Note:
A class cannot implement two interfaces that
share function names, since it would cause
ambiguity.
40. Interfaces provides a form of multiple
inheritance . An abstract class can extend one
other class.
A class may implements severeal interface.
But incase of abstract class, class may extend
only one single abstract class.
Interfaces are slow as it requires extra
indirection to find corresponding method in
the actual class where abstract class are fast.
41. Php 5 introduces the final keyword, which
prevents child classes from overriding a
method by prefixing the definition with final.
If the class itself is being defined final then it
can not be extended.
Properties can’t be declared final, only classes
and methods may be declared as final.
42.
43. It is used to test if an object is an instance of
a particular class. This operator returns true if
the object instance has the named class
anywhere in its parent tree.
44. <?php
class a
{
}
class Mammal extends a
{
}
class Human extends Mammal
{
}
$obj = new Human;
echo ($obj instanceof a ?'true':'false';
?>