You are on page 1of 13

Q1)

Class:
 A class is a template or blueprint for creating objects.
 It defines the properties (variables) and methods (functions) that will be associated with the
objects created from it.
 Classes encapsulate data for the object and provide methods for interacting with that data.
 They serve as a logical grouping of related data and functionality.

Object:
 An object is an instance of a class.
 It represents a real-world entity that can have its own unique properties and behaviors based
on the class blueprint.
 Objects are created using the new keyword followed by the class name.

Q2)
Step 1, Declare a class
<?php
class Book {
// Properties
public $title;
public $author;
public $publishedYear;

// Constructor
public function __construct($title, $author, $publishedYear) {
$this->title = $title;
$this->author = $author;
$this->publishedYear = $publishedYear;
}

// Method to get a summary of the book


public function getSummary() {
return "{$this->title} written by {$this->author} in {$this->publishedYear}.";
}
}
?>
Step 2, Create an Object
<?php
// Include the class definition
require_once 'Book.php';

// Creating an object of the Book class


$book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925);

// Accessing properties directly


echo "Book title: " . $book1->title . "<br>";
echo "Book author: " . $book1->author . "<br>";
echo "Published year: " . $book1->publishedYear . "<br>";

// Calling a method of the object


echo "Book Summary: " . $book1->getSummary();
?>
Q3)
1. Class Declaration Rules:
 Class names in PHP must start with a letter or underscore, followed by any
combination of letters, numbers, or underscores.
 Class names are case-insensitive in PHP.
 The class declaration should be enclosed within <?php ?> tags.
 Each class should ideally be saved in its own file, and the file name should match the
class name. For example, MyClass should be saved in MyClass.php.
 A class can only extend one parent class (single inheritance), but it can implement
multiple interfaces (multiple inheritance through interfaces).
2. Object Creation Rules:
 Objects are created using the new keyword followed by the class name and
parentheses (if the class has a constructor).
 Objects can be assigned to variables, passed as function arguments, or returned from
functions.
 The constructor method (__construct()) is called automatically when an object is
created. It's used for initializing object properties or performing any setup tasks.
 Object properties and methods can be accessed using the object operator (->).
3. Access Control:
 PHP provides visibility modifiers (public, protected, private) to control the
accessibility of properties and methods.
 public: accessible from anywhere, both inside and outside the class.
 protected: accessible from within the class and its subclasses.
 private: accessible only from within the class itself.
 Properties and methods without an explicit visibility modifier default to public.
4. Naming Conventions:
 Class names typically follow the StudlyCaps convention, also known as CamelCase,
where each word in the class name begins with a capital letter.
 Property and method names usually follow the camelCase convention, starting with a
lowercase letter and using capital letters for each subsequent word (e.g., myProperty,
myMethod()).
 Constants within classes are often declared using uppercase letters with underscores
separating words (e.g., MY_CONSTANT).
5. Inheritance and Polymorphism:
 PHP supports class inheritance, allowing a subclass to inherit properties and methods
from a parent class. This promotes code reuse and allows for the implementation of
polymorphism.
 Methods in a subclass can override methods with the same name and signature in the
parent class, allowing for polymorphic behavior.
 The parent:: keyword can be used to access overridden methods or properties from
within a subclass.
Q4)
To enhance the PHP program created in the individual assignment using object-oriented principles, Its
by adopting a structured approach through the implementation of classes and objects. By doing so, we
introduce several benefits including modularity, encapsulation, code reusability, readability, and
abstraction.

Q5)
<?php
session_start();
error_reporting(0);
include('includes/config.php');
// Code user Registration
if(isset($_POST['submit']))
{
$name=$_POST['fullname'];
$email=$_POST['emailid'];
$contactno=$_POST['contactno'];
$password=md5($_POST['password']);
$query=mysqli_query($con,"insert into users(name,email,contactno,password)
values('$name','$email','$contactno','$password')");
if($query)
{
echo "<script>alert('You are successfully register');</script>";
}
else{
echo "<script>alert('Not register something went worng');</script>";
}
}
// Code for User login
if(isset($_POST['login']))
{
$email=$_POST['email'];
$password=md5($_POST['password']);
$query=mysqli_query($con,"SELECT * FROM users WHERE email='$email' and
password='$password'");
$num=mysqli_fetch_array($query);
if($num>0)
{
$extra="index.php";
$_SESSION['login']=$_POST['email'];
$_SESSION['id']=$num['id'];
$_SESSION['username']=$num['name'];

$host=$_SERVER['HTTP_HOST'];
$uri=rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
header("location:http://$host$uri/$extra");
exit();
}
else
{
$extra="login.php";
$email=$_POST['email'];
$uip=$_SERVER['REMOTE_ADDR'];

$host = $_SERVER['HTTP_HOST'];
$uri = rtrim(dirname($_SERVER['PHP_SELF']),'/\\');
header("location:http://$host$uri/$extra");
$_SESSION['errmsg']="Invalid email id or Password";
exit();
}
}

?>

<!DOCTYPE html>
<html lang="en">
<head>
<!-- Meta -->
<meta charset="utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-
scalable=no">
<meta name="robots" content="all">

<title>e-Commerce</title>
<!-- Bootstrap Core CSS -->
<link rel="stylesheet" href="assets/css/bootstrap.min.css">

<!-- Customizable CSS -->


<link rel="stylesheet" href="assets/css/main.css">
<link rel="stylesheet" href="assets/css/green.css">
<link rel="stylesheet" href="assets/css/owl.carousel.css">
<link rel="stylesheet" href="assets/css/owl.transitions.css">
<!--<link rel="stylesheet" href="assets/css/owl.theme.css">-->
<link href="assets/css/lightbox.css" rel="stylesheet">
<link rel="stylesheet" href="assets/css/animate.min.css">
<link rel="stylesheet" href="assets/css/rateit.css">
<link rel="stylesheet" href="assets/css/bootstrap-select.min.css">

<!-- Demo Purpose Only. Should be removed in production -->


<link rel="stylesheet" href="assets/css/config.css">

<link href="assets/css/green.css" rel="alternate stylesheet" title="Green color">


<link href="assets/css/blue.css" rel="alternate stylesheet" title="Blue color">
<link href="assets/css/red.css" rel="alternate stylesheet" title="Red color">
<link href="assets/css/orange.css" rel="alternate stylesheet" title="Orange color">
<link href="assets/css/dark-green.css" rel="alternate stylesheet" title="Darkgreen
color">
<!-- Demo Purpose Only. Should be removed in production : END -->

<!-- Icons/Glyphs -->


<link rel="stylesheet" href="assets/css/font-awesome.min.css">

<!-- Fonts -->


<link href='http://fonts.googleapis.com/css?family=Roboto:300,400,500,700'
rel='stylesheet' type='text/css'>
<script type="text/javascript">
function valid()
{
if(document.register.password.value!= document.register.confirmpassword.value)
{
alert("Password and Confirm Password Field do not match !!");
document.register.confirmpassword.focus();
return false;
}
return true;
}
</script>
<script>
function userAvailability() {
$("#loaderIcon").show();
jQuery.ajax({
url: "check_availability.php",
data:'email='+$("#email").val(),
type: "POST",
success:function(data){
$("#user-availability-status1").html(data);
$("#loaderIcon").hide();
},
error:function (){}
});
}
</script>
</head>
<body class="cnt-home">

<!-- ============================================== HEADER


============================================== -->
<header class="header-style-1">

<!-- ============================================== TOP MENU


============================================== -->
<?php include('includes/top-header.php');?>
<!-- ============================================== TOP MENU : END
============================================== -->
<?php include('includes/main-header.php');?>
<!-- ============================================== NAVBAR
============================================== -->
<?php include('includes/menu-bar.php');?>
<!-- ============================================== NAVBAR : END
============================================== -->

</header>

<!-- ============================================== HEADER : END


============================================== -->
<div class="breadcrumb">
<div class="container">
<div class="breadcrumb-inner">
<ul class="list-inline list-unstyled">
<li><a href="home.html">Home</a></li>
<li class='active'>Authentication</li>
</ul>
</div><!-- /.breadcrumb-inner -->
</div><!-- /.container -->
</div><!-- /.breadcrumb -->

<div class="body-content outer-top-bd">


<div class="container">
<div class="sign-in-page inner-bottom-sm">
<div class="row">
<!-- Sign-in -->
<div class="col-md-6 col-sm-6 sign-in">
<h4 class="">sign in</h4>
<p class="">Hello, Welcome to your account.</p>
<form class="register-form outer-top-xs" method="post">
<span style="color:red;" >
<?php
echo htmlentities($_SESSION['errmsg']);
?>
<?php
echo htmlentities($_SESSION['errmsg']="");
?>
</span>
<div class="form-group">
<label class="info-title" for="exampleInputEmail1">Email Address
<span>*</span></label>
<input type="email" name="email" class="form-control unicase-form-control text-
input" id="exampleInputEmail1" >
</div>
<div class="form-group">
<label class="info-title" for="exampleInputPassword1">Password
<span>*</span></label>
<input type="password" name="password" class="form-control unicase-form-
control text-input" id="exampleInputPassword1" >
</div>
<div class="radio outer-xs">
<a href="forgot-password.php" class="forgot-password pull-right">Forgot
your Password?</a>
</div>
<button type="submit" class="btn-upper btn btn-primary checkout-page-button"
name="login">Login</button>
</form>
</div>
<!-- Sign-in -->

<!-- create a new account -->


<div class="col-md-6 col-sm-6 create-new-account">
<h4 class="checkout-subtitle">create a new account</h4>
<p class="text title-tag-line">Create your own Shopping account.</p>
<form class="register-form outer-top-xs" role="form" method="post" name="register"
onSubmit="return valid();">
<div class="form-group">
<label class="info-title" for="fullname">Full Name <span>*</span></label>
<input type="text" class="form-control unicase-form-control text-input"
id="fullname" name="fullname" required="required">
</div>

<div class="form-group">
<label class="info-title" for="exampleInputEmail2">Email Address
<span>*</span></label>
<input type="email" class="form-control unicase-form-control text-input" id="email"
onBlur="userAvailability()" name="emailid" required >
<span id="user-availability-status1" style="font-size:12px;"></span>
</div>

<div class="form-group">
<label class="info-title" for="contactno">Contact No. <span>*</span></label>
<input type="text" class="form-control unicase-form-control text-input"
id="contactno" name="contactno" maxlength="10" required >
</div>
<div class="form-group">
<label class="info-title" for="password">Password. <span>*</span></label>
<input type="password" class="form-control unicase-form-control text-input"
id="password" name="password" required >
</div>

<div class="form-group">
<label class="info-title" for="confirmpassword">Confirm Password.
<span>*</span></label>
<input type="password" class="form-control unicase-form-control text-input"
id="confirmpassword" name="confirmpassword" required >
</div>

<button type="submit" name="submit" class="btn-upper btn btn-primary checkout-


page-button" id="submit">Sign Up</button>
</form>

</div>
<!-- create a new account --> </div><!-- /.row -->
</div>

</div>
</div>

<script src="assets/js/jquery-1.11.1.min.js"></script>

<script src="assets/js/bootstrap.min.js"></script>

<script src="assets/js/bootstrap-hover-dropdown.min.js"></script>
<script src="assets/js/owl.carousel.min.js"></script>

<script src="assets/js/echo.min.js"></script>
<script src="assets/js/jquery.easing-1.3.min.js"></script>
<script src="assets/js/bootstrap-slider.min.js"></script>
<script src="assets/js/jquery.rateit.min.js"></script>
<script type="text/javascript" src="assets/js/lightbox.min.js"></script>
<script src="assets/js/bootstrap-select.min.js"></script>
<script src="assets/js/wow.min.js"></script>
<script src="assets/js/scripts.js"></script>

<!-- For demo purposes – can be removed on production -->

<script src="switchstylesheet/switchstylesheet.js"></script>

<script>
$(document).ready(function(){
$(".changecolor").switchstylesheet( { seperator:"color"} );
$('.show-theme-options').click(function(){
$(this).parent().toggleClass('open');
return false;
});
});

$(window).bind("load", function() {
$('.show-theme-options').delay(2000).trigger('click');
});
</script>
<!-- For demo purposes – can be removed on production : End -->

</body>
</html>

Q6)
Use of Classes
We can encapsulate related functionalities into classes. For instance, a User class can be created to
handle user-related properties and methods, such as registration and login.
Before:
Procedural handling of user registration and login.
After:
class User {
private $db;

public function __construct($db) {


$this->db = $db;
}

public function register($name, $email, $contactno, $password) {


// Implementation using $this->db
}

public function login($email, $password) {


// Implementation using $this->db
}

// Other user-related methods...


}

You might also like