The document discusses object oriented concepts in PHP. It provides 22 steps for developing an application using object oriented principles in PHP. Some key points covered include:
- Creating PHP classes to organize code into reusable blueprints
- Defining properties and methods within classes
- Instantiating objects from classes and accessing object properties and methods
- Using constructors to initialize properties when objects are created
- Implementing getter and setter methods to encapsulate property access
- Applying access modifiers like public, private, and protected to restrict property and method access
The steps provide a guide for designing classes, creating objects, setting and getting property values, and controlling access to properties and methods through access modifiers. This allows for developing PHP applications
For beginners who want to go to the next level of OOP in PHP, we talk about magic methods, iterators, filters, some parts of the SPL, the subject/observer pattern and how you can build classes that have a lot of power and responsibility by implementing interfaces and then type-hinting and using just methods that are defined in the interface. Prefer composition over inheritance.
The PPTs from one of the event of iWillStudy.com - a leading start-up in the education space in India. This PPT is being used at an event where they taught iPhone programming and applications development.
Object Oriented Programming Basics with PHPDaniel Kline
explanation of object oriented programming, differences between procedural programming and OOP, classes vs. objects, class constants and internal reference, public vs. private scope, copying and cloning objects
JavaScript is the language of Web. It has its own styles and vocabulary, which is very different from languages we are familiar with like C, C++, Java, C# etc. While not having classes and functions to be acting as FIRST CLASS CITIZENS for most of the tasks sometimes its difficult to manage very large JavaScript files. And, there comes the need of a common pattern for writing efficient JavaScript.
For beginners who want to go to the next level of OOP in PHP, we talk about magic methods, iterators, filters, some parts of the SPL, the subject/observer pattern and how you can build classes that have a lot of power and responsibility by implementing interfaces and then type-hinting and using just methods that are defined in the interface. Prefer composition over inheritance.
The PPTs from one of the event of iWillStudy.com - a leading start-up in the education space in India. This PPT is being used at an event where they taught iPhone programming and applications development.
Object Oriented Programming Basics with PHPDaniel Kline
explanation of object oriented programming, differences between procedural programming and OOP, classes vs. objects, class constants and internal reference, public vs. private scope, copying and cloning objects
JavaScript is the language of Web. It has its own styles and vocabulary, which is very different from languages we are familiar with like C, C++, Java, C# etc. While not having classes and functions to be acting as FIRST CLASS CITIZENS for most of the tasks sometimes its difficult to manage very large JavaScript files. And, there comes the need of a common pattern for writing efficient JavaScript.
To better understand the behavior of servlets, let’s take a look at the life cycle of servlets.
A servlet is basically a small Java program that runs within a Web server. It can receive requests from clients and return responses. The whole life cycle of a servlet breaks up into 3 phases:
• Initialization: A servlet is first loaded and initialized usually when it is requested by the corresponding clients. Some websites allow the users to load and initialize servlets when the server is started up so that the first request will get responded more quickly.
• Service: After initialization, the servlets serve clients on request, implementing the ap- plication logic of the web application they belong to.
• Destruction: When all pending requests are processed and the servlets have been idle for a specific amount of time, they may be destroyed by the server and release all the resources they occupy.
More specifically, the behavior of a servlet is described in javax.servlet.Servlet interface, in which the following methods are defined:
• public void init(ServletConfig config) throws ServletException
This method is called once when the servlet is loaded into the servlet engine, before the servlet is asked to process its first request.
The init method has a ServletConfig parameter. The servlet can read its initialization arguments through the ServletConfig object. How the initialization arguments are set is servlet engine dependent but they are usually defined in a configuration file.
A typical example of an initialization argument is a database identifier. A servlet can read this argument from the ServletConfig at initialization and then use it later to open a connection to the database during processing of a request:
private String databaseURL;
public void init(ServletConfig config) throws ServletException { super.init(config);
databaseURL = config.getInitParameter("database");
}
• public void service(ServletRequest request, ServletResponse response) throws ServletException, IOException
This method is called to process a request. It can be called zero, one or many times until the servlet is unloaded.
Once a servlet is loaded, it remains in the server’s memory as a single object instance. Thereafter, the server invokes the servlet to handle a request using a simple, lightweight method invocation. Unlike with CGI, there’s no process to spawn or interpreter to invoke, so the servlet can begin handling the request almost immediately. Multiple, concurrent requests are handled by separate threads, so servlets are highly scalable.
Servlets are naturally enduring objects. Because a servlet stays in the server’s memory as a single object instance, it automatically maintains its state and can hold on to external resources, such as database connections, that may otherwise take several seconds to establish. The following servlet presents information about how many times it has been accessed:
To better understand the behavior of servlets, let’s take a lo
It contains basic fundamental of class, object, method overloading, constructor, destructor, visibility, acess specifier, Methods, overloading, clonning of objects etc. are covered here.
Kindly look at it and give your comments to improve it as good as possible ways.
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
Dev Dives: Train smarter, not harder – active learning and UiPath LLMs for do...UiPathCommunity
💥 Speed, accuracy, and scaling – discover the superpowers of GenAI in action with UiPath Document Understanding and Communications Mining™:
See how to accelerate model training and optimize model performance with active learning
Learn about the latest enhancements to out-of-the-box document processing – with little to no training required
Get an exclusive demo of the new family of UiPath LLMs – GenAI models specialized for processing different types of documents and messages
This is a hands-on session specifically designed for automation developers and AI enthusiasts seeking to enhance their knowledge in leveraging the latest intelligent document processing capabilities offered by UiPath.
Speakers:
👨🏫 Andras Palfi, Senior Product Manager, UiPath
👩🏫 Lenka Dulovicova, Product Program Manager, UiPath
UiPath Test Automation using UiPath Test Suite series, part 3DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 3. In this session, we will cover desktop automation along with UI automation.
Topics covered:
UI automation Introduction,
UI automation Sample
Desktop automation flow
Pradeep Chinnala, Senior Consultant Automation Developer @WonderBotz and UiPath MVP
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
The Art of the Pitch: WordPress Relationships and SalesLaura Byrne
Clients don’t know what they don’t know. What web solutions are right for them? How does WordPress come into the picture? How do you make sure you understand scope and timeline? What do you do if sometime changes?
All these questions and more will be explored as we talk about matching clients’ needs with what your agency offers without pulling teeth or pulling your hair out. Practical tips, and strategies for successful relationship building that leads to closing the deal.
DevOps and Testing slides at DASA ConnectKari Kakkonen
My and Rik Marselis slides at 30.5.2024 DASA Connect conference. We discuss about what is testing, then what is agile testing and finally what is Testing in DevOps. Finally we had lovely workshop with the participants trying to find out different ways to think about quality and testing in different parts of the DevOps infinity loop.
Smart TV Buyer Insights Survey 2024 by 91mobiles.pdf91mobiles
91mobiles recently conducted a Smart TV Buyer Insights Survey in which we asked over 3,000 respondents about the TV they own, aspects they look at on a new TV, and their TV buying preferences.
UiPath Test Automation using UiPath Test Suite series, part 4DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 4. In this session, we will cover Test Manager overview along with SAP heatmap.
The UiPath Test Manager overview with SAP heatmap webinar offers a concise yet comprehensive exploration of the role of a Test Manager within SAP environments, coupled with the utilization of heatmaps for effective testing strategies.
Participants will gain insights into the responsibilities, challenges, and best practices associated with test management in SAP projects. Additionally, the webinar delves into the significance of heatmaps as a visual aid for identifying testing priorities, areas of risk, and resource allocation within SAP landscapes. Through this session, attendees can expect to enhance their understanding of test management principles while learning practical approaches to optimize testing processes in SAP environments using heatmap visualization techniques
What will you get from this session?
1. Insights into SAP testing best practices
2. Heatmap utilization for testing
3. Optimization of testing processes
4. Demo
Topics covered:
Execution from the test manager
Orchestrator execution result
Defect reporting
SAP heatmap example with demo
Speaker:
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
Epistemic Interaction - tuning interfaces to provide information for AI supportAlan Dix
Paper presented at SYNERGY workshop at AVI 2024, Genoa, Italy. 3rd June 2024
https://alandix.com/academic/papers/synergy2024-epistemic/
As machine learning integrates deeper into human-computer interactions, the concept of epistemic interaction emerges, aiming to refine these interactions to enhance system adaptability. This approach encourages minor, intentional adjustments in user behaviour to enrich the data available for system learning. This paper introduces epistemic interaction within the context of human-system communication, illustrating how deliberate interaction design can improve system understanding and adaptation. Through concrete examples, we demonstrate the potential of epistemic interaction to significantly advance human-computer interaction by leveraging intuitive human communication strategies to inform system design and functionality, offering a novel pathway for enriching user-system engagements.
2. PHP 4, PHP 5 & PHP 6
There are substantial differences between PHP 4 and
PHP 5. Most of the hype was around the new object
model, which was completely rewritten in PHP5. The PHP
5 version is much more complete, and performs much
better as well. In PHP 4, objects were really just primitive
data types, and were referenced by value. In an attempt
to retain as much backward compatibility as possible in
PHP 5 allows compatibility with the version 4 methods.
With the release of PHP 5 in 2004, PHP programmers
finally had the power to code like the Java and C#, PHP
finally had a complete OOP infrastructure.
PHP 6 has more features of object Oriented Concepts.
3. Step by Step Process
The difference between building a PHP application the old
fashioned (procedural) way versus the OOP way.
What the basic OOP principles are, and how to use them in
PHP?
When to use OOP in your PHP scripts?
4. Object Oriented PHP
How to develop a OO PHP ?? to get into this we are going to divide the
process into 22 steps by which we can get a basic idea to develop an
application in OOP Concepts.
STEP 1
First lets create 2 PHP files
index.php
class_lib.php
OOP is all about creating modular code, so our object oriented PHP
code will be contained in dedicated files that we will then insert into our
normal PHP page using PHP 'includes'.
In this case, all our OO PHP code will be in the PHP file: class_lib.php
In OOP codes revolves around a 'class', Classes are the templates that
are used to define objects.
STEP 2
Create a simple PHP class (in class_lib.php)
Instead of having a bunch of functions, variables and code floating
around, to design our PHP scripts in the OOP way, we need to create
our own classes.
keyword 'class'
5. STEP 2 ( conti ...)
<?php
class classname
{
}
?>
STEP 3 (add data to your class)
Classes are the blueprints for php objects. One of the big differences
between functions and classes is that a class contains both data (variables)
and functions that form a package called an: 'object'.
When you create a variable inside a class, it is called a 'property'.
<?php
class classname
{
// var $name is called as properties of class var keyword
var $name;
}
?>
6. STEP 4 (add functions/methods to your class)
Functions also referred by different name when created inside a class - they
are called 'methods'.
A class's methods are used to manipulate its own data / properties.
<?php
class mfs_employee
{
var $name;
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
?>
7. STEP 5 (getter and setter functions)
We've created two interesting functions/methods: get_name() and set_name().
These methods follow a common OOP convention that you see in many
languages (including Java and Ruby) - where you create methods to 'set' and
'get' properties in a class.
NOTE : Another convention (a naming convention,) is that getter and setter
names should match the property names.
This way, when other PHP programmers want to use your objects, they will
know that if you have a method/function called 'set_name()', there will be a
property/variable called 'name'.
<?php
class mfs_employee
{
var $name;
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
?>
8. STEP 6 (The '$this' variable)
$this->name = $new_name;
$this is a built-in variable which points to the current object. Or in other
words, $this is a special self-referencing variable. We use $this to access
properties and to call other methods of the current class.
STEP 7 (Use our class in our main PHP page : index.php )
We should not create the PHP classes in our main page, else it will
break the main purpose of building applications in OOP.
So in index.php include the file ( class_lib.php )
<?php include('class_lib.php'); ?>
9. STEP 8 ( Instantiate/create your object )
Classes are the blueprints/templates of php objects. Classes don't
actually become objects until you do something called: instantiation.
When you instantiate a class, you create an instance of it ... thus
creating the object.
In other words, instantiation is the process of creating an instance
of an object in memory. What memory? The server's memory of
course!
<?php
$obj_mfsemp = new mfs_employee();
?>
Note: The variable $obj_mfsemp becomes a handle/reference to
our newly created mfs_employee class. It is a 'handle', because we
will use $obj_mfsemp to control and use the mfs_employee class.
10. STEP 9 ( new keyword )
To create an object out of a class, you need to use the 'new'
keyword.
When creating/instantiating a class, we can optionally add brackets
to the class name, as below example. To be clear, we can see in the
code below how we create multiple objects from the same class.
From the PHP's engine point of view, each object is its own entity.
<?php
$obj_mfsemp1 = new mfs_employee ();
$obj_mfsemp2 = new mfs_employee ;
?>
11. STEP 10 ( Set an objects properties )
Now that we've created/instantiated our two separate
'mfs_employee' objects, we can set their properties using the
methods (the setters) we created.
Please keep in mind that though both our mfs_employee objects
($obj_mfsemp1 and $obj_mfsemp2) are based on the same
'mfs_employee' class, as far as php is concerned, they are totally
different objects.
<?php
$obj_mfsemp1 = new mfs_employee ();
$obj_mfsemp2 = new mfs_employee ;
$obj_mfsemp1->set_name("Abinash Grahacharya");
$obj_mfsemp2->set_name("Amitabh Pattnaik");
?>
12. STEP 11 ( Accessing an object's data )
Now we use the getter methods to access the data held in our
objects … this is the same data we inserted into our objects
using the setter methods.
When accessing methods and properties of a class, we use the
arrow (->) operator.
<?php
$obj_mfsemp1 = new mfs_employee ();
$obj_mfsemp2 = new mfs_employee ;
//setting values in the object
$obj_mfsemp1->set_name("Abinash Grahacharya");
$obj_mfsemp2->set_name("Amitabh Pattnaik");
//getting each values from the object
echo $obj_mfsemp1 -> get_name();
echo "<br />";
echo $obj_mfsemp2 -> get_name();
?>
13. In this short period of time, we have covered
Designed a PHP class.
Generate/created a couple of objects based on your class.
Inserted data into your objects.
Retrieved data from your objects.
Lets now focus on PHP OBJECT.
14. STEP 12 ( Directly accessing properties - don't do it! )
We don't have to use methods to access objects properties; you can
directly get to them using the arrow operator (->) and the name of
the variable.
For example: with the property $name (in object $obj_mfsemp1,) we
can get its' value like :
<?php
echo $obj_mfsemp1->name;
?>
NOTE : Though doable, it is considered bad practice to do it
because it can lead to trouble down the road. We should use getter
methods instead.
15. STEP 13 ( Constructor )
All objects can have a special built-in method called a 'constructor'. Constructors
allow you to initialize your object's properties (give values to properties) when we
instantiate (create) an object.
Note: If you create a __construct() function PHP will automatically call the
__construct() method/function when you create an object from your class.
The 'construct' method starts with two underscores (__) and the word 'construct'.
<?php
class mfs_employee
{
var $name;
function __construct($con_name)
{
$this->name = $con_name;
}
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
?>
16. STEP 14 ( Create an object with a constructor )
Now that we've created a constructor method, we can provide a
value for the $name property when we create our objects for the
class mfs_employee.
We 'feed' the constructor method by providing a list of arguments
(like we do with a function) after the class name at the time of object
declaration.
Not a constructor
<?php
$obj_mfsemp1 = new mfs_employee ();
?>
When have constructor
<?php
$obj_con_mfsemp3 = new mfs_employee (“Abinash Grahacharya”);
?>
17. STEP 15 ( access modifiers )
One of the fundamental principles in OOP is 'encapsulation'. The
idea is that we create cleaner better code, if you restrict access to
the data structures (properties) in our objects.
Encapsulation : Storing data/properties and functions/methods in a
single unit (class) is encapsulation. Data cannot be accessible to
the outside world and only those functions which are stored in the
class can access it.
We restrict access to class properties using something called
'access modifiers'. There are 3 access modifiers:
1. public
2. private
3. protected
'Public' is the default modifier.
18. STEP 15 ( access modifiers ) conti...
<?php
class mfs_employee
{
var $name;
public $designation = 'SW Engineer';
protected $standard_charted_pin = '756472';
private $gps_password = 'mindfire';
function __construct($con_name)
{
$this->name = $con_name;
}
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
//NOTE : when ever we are using var it is treated as public
?>
19. STEP 16 ( Restricting access to properties )
Properties declared as 'public' have no access restrictions, meaning anyone
can access them.
When you declare a property as 'private', only the same class can access the
property.
When a property is declared 'protected', only the same class and classes
derived from that class can access the property - this has to do with inheritance
<?php
$obj_mfsemp1 = new mfs_employee (“Mindfire”);
echo $obj_mfsemp1-> get_name();
//when we try to access private or public properties outside class will through Fatal
Error
echo $obj_mfsemp1-> standard_charted_pin;
?>
20. STEP 17 ( Restricting access to methods )
Like properties, you can control access to methods using one of the three
access modifiers:
1. public
2. protected
3. private
<?php
class mfs_employee
{
var $name;
public $designation = 'SW Engineer';
protected $standard_charted_pin = '756472';
private $gps_password = 'mindfire';
private function getpin()
{
return $this->standard_charted_pin ;
}
}
?>
Since the method getpin() is 'private', the only place you can use this method is in
the same class - typically in another method in class. If we wanted to call/use this
method directly in our PHP pages, we need to declare it as 'public'.
21. STEP 18 ( Inheritance - reusing code the OOP way )
Inheritance is a fundamental capability/construct in OOP where you can
use one class, as the base/basis for another class … or many other
classes.
Why do it?
Doing this allows help to efficiently reuse the code found in our base class.
Say, you wanted to create a new 'sales_people' class … since we can say
that 'mfs_employee' is a type/kind of 'peoples', they will share common
properties and methods.
In this type of situation, inheritance can make our code lighter … because
we are reusing the same code in two different classes.
1. You only have to type the code out once.
2. The actual code being reused, can be reused in many classes but it is
only typed out in one place … conceptually, this is sort-of like PHP
includes().
22. STEP 18 ( Inheritance - reusing code the OOP way ) conti..
// 'extends' is the keyword that enables
inheritance
class sales_people extends mfs_employee
{
function __construct($employee_name)
{
$this ->
set_name($employee_name);
}
}
<?php
class mfs_employee
{
var $name;
public $designation = 'SW Engineer';
protected $standard_charted_pin = '756472';
private $gps_password = 'mindfire';
function __construct($con_name)
{
$this->name = $con_name;
}
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
//NOTE : when ever we are using var it is treated as public
?>
23. STEP 19 ( Inheritance - reusing code the OOP way how to access )
Because the class 'sales_people' is based on the class ' mfs_employee ',
'sales_people' automatically has all the public and protected, properties
and methods of 'mfs_employee' class.
Notice how we are able to use set_name() in 'sales_people', even though we
did not declare that method in the 'sales_people' class. That's because we
already created set_name() in the class 'mfs_employee'.
Note: the 'sales_people' class is called children the 'base' class or the
'mfs_employee' class because it's the class that the 'sales_people' is based
on. This class hierarchy can become important down the road when our
projects become more complex.
// 'extends' is the keyword that enables
inheritance
class sales_people extends mfs_employee
{
function __construct($employee_name)
{
$this ->
set_name($employee_name);
}
}
24. STEP 20 ( Inheritance - reusing code the OOP way- How to access )
<?php
class mfs_employee
{
var $name;
public $designation = 'SW Engineer';
protected $standard_charted_pin = '756472';
private $gps_password = 'mindfire';
function __construct($con_name)
{
$this->name = $con_name;
}
function set_name($new_name)
{
$this->name = $new_name;
}
function get_name()
{
return $this->name;
}
}
//NOTE : when ever we are using var it is treated as public
?>
// 'extends' is the keyword that enables inheritance
class sales_people extends mfs_employee
{
function __construct($employee_name)
{
$this -> set_name($employee_name);
}
}
//In PHP file
<?php
$sp_obj_c2 = new sales_people("class2 names");
echo $sp_obj_c2 -> get_name() ;
?>
This is a classic example of how OOP
can reduce the number of lines of code
(don't have to write the same methods
twice) while still keeping your code
modular and much easier to maintain.
25. STEP 21 ( Overriding Methods )
Sometimes (when using inheritance,) we may need to change how a
method works from the base class.
For example, let's say set_name() method in the 'sales_people' class, have
to do something different than what it does in the 'mfs_employee' class.
We have to 'override' the ''mfs_employee' classes version of set_name(),
by declaring the same method in 'sales_people'.
// 'extends' is the keyword that enables inheritance
class sales_people extends mfs_employee
{
function __construct($employee_name)
{
$this -> set_name($employee_name);
}
function set_name($new_name)
{
if ($new_name[0] == "S")
{
$this->name = $new_name;
}
}
}
//In PHP file
<?php
$sp_obj_c2 = new sales_people("class2 names");
echo $sp_obj_c2 -> get_name() ;
$sp_obj_c2 = new sales_people("So Check it");
echo $sp_obj_c2 -> get_name() ;
?>
26. STEP 22 ( Overriding Methods ) cont..
Sometimes we may need to access our base class's version of a method over
lode in the derived (sometimes called 'child') class.
In our example, we overrode the set_name() method in the 'sales_people' class.
Now We have to used the following code :
mfs_employee::set_name($new_name);
to access the parent class' (mfs_employee) version of the set_name() method
// 'extends' is the keyword that enables inheritance
class sales_people extends mfs_employee
{
function __construct($employee_name)
{
$this -> set_name($employee_name);
}
function set_name($new_name)
{
if ($new_name == "Stefan Sucks")
{
$this->name = $new_name;
}
}
function set_name_old_style($new_name)
{
mfs_employee::set_name($new_name);
}
}
:: will tell to PHP to search for set_name() in
the 'base' class.
27. STEP 22 ( Overriding Methods ) cont..
Also by using the parent keyword we can call the parent methods if it is overloaded
// 'extends' is the keyword that enables inheritance
class sales_people extends mfs_employee
{
function __construct($employee_name)
{
$this -> set_name($employee_name);
}
function set_name($new_name)
{
if ($new_name == "Stefan Sucks")
{
$this->name = $new_name;
}
}
function set_name_old_style($new_name)
{
parent::set_name($new_name);
}
}