0% found this document useful (0 votes)
15 views12 pages

Accessing Properties and Methods 2.1.2

The document outlines a course on Web Technologies focusing on PHP and WordPress, detailing learning outcomes related to PHP basics, themes in WordPress, and database connectivity. It includes information on accessing properties and methods in PHP classes, along with access specifiers (public, private, protected) and their implications. Additionally, it provides examples of class declarations and methods, as well as references for further reading.

Uploaded by

siyalohiya67
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views12 pages

Accessing Properties and Methods 2.1.2

The document outlines a course on Web Technologies focusing on PHP and WordPress, detailing learning outcomes related to PHP basics, themes in WordPress, and database connectivity. It includes information on accessing properties and methods in PHP classes, along with access specifiers (public, private, protected) and their implications. Additionally, it provides examples of class declarations and methods, as well as references for further reading.

Uploaded by

siyalohiya67
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 12

UNIVERSITY INSTITUTE OF COMPUTING

Bachelor of Computer Application


Subject Name: Web Technologies
22CAH-371/23SCH-254

Web Technologies DISCOVER . LEARN . EMPOWER


1
Web Technologies
• Web development
Course Outcome
CO Title Level
• History
Number • Static pages
CO1 Understand • Dynamic pages
Students will learn basics of PHP

CO2 Understand &


Students will understand the underlying concept of themes in Analyze
wordpress.

CO3 Understand
Students will learn database connectivity, plugin and security
issues.

2
Web Development
Using PHP
Course Outcome • OOP
CO Title Level
Number
• Session and Cookies
Understand
• File handling
CO1
Students will learn basics of PHP • Database
CO2 Understand &
Students will understand the underlying concept of themes in Analyze
wordpress.

CO3 Understand
Students will learn database connectivity, plugin and security
issues.

3
Topic to be Covered
• Accessing properties and methods

4
Accessing properties and methods

• Class member variables are called properties. Sometimes they are referred as attributes
or fields.
• The properties hold specific data and related with the class in which it has been defined.
• Declaring a property in a class is an easy task, use one of the keyword public, protected,
or private followed by a normal variable declaration. If declared using var (compatibility
with PHP 4), the property will be defined as public.

• public : The property can be accessed from outside the class, either by the script or from another
class
• private : No access is granted from outside the class, either by the script or from another class.
• protected : No access is granted from outside the class except a class that’s a child of the class
with the protected property or method.

5
Example:-

After an object is instantiated, you can access the property of a class using
the object and -> operator. Any member declared with keyword "private" or "protected"
cannot be accessed outside the method of the class.

<?php
class Myclass
{
public $font_size =10;
}
$f = new MyClass;
echo $f->font_size; 6
Accessing Methods
• The functions which are declared in a class are called methods.
• A class method is exactly similar to PHP functions.
• Declaring a method in a class is an easy task, use one of the keyword public, protected, or private followed
by a method name.

• public : The method can be accessed from outside the class.


• private : No access is granted from outside the class.
• protected : No access is granted from outside the class except a class that’s a child of the class with the
protected property or method.
• A valid method name starts with a letter or underscore, followed by any number of letters, numbers, or
underscores.
• The method body enclosed within a pair of braces which contains codes. The opening curly brace ( { )
indicates the beginning of the method code and the closing curly ( } ) brace indicates the termination of the
method.
• If the method is not defined by public, protected, or private then default is public.
• Can access properties and methods of the current instance using $this (Format $this->property) for non
static property. 7
Example
<?php

//Declare the class


class Product
{
//Declare properties
public $name ="HP Pavillion";
public $type = "Laptop";
public $price = 1200;

//Declare method to print the properties


public function details()
{
echo "Name :".$this->name."<br />"."Type :".$this->type."<br />"."Price :$".$this->price."<br />";
}
}
//Declare the object

$object = new Product();


//Call the method
echo $object->details();
?>

8
Access Specifiers

• Public: Public properties can be accessed by any code, whether that


code is inside or outside the class. If a property is declared public, its
value can be read or changed from anywhere in your script.
• Private: Private properties of a class can be accessed only by code
inside the class. So if we create a property that’s declared private, only
methods and objects inside the same class can access its contents.
• Protected: Protected class properties are a bit like private properties in
that they can’t be accessed by code outside the class, but there’s one
little difference in any class that inherits from the class i.e. base class
can also access the properties.

9
Class Member Accessible from derived
Access from own class Accessible by Object
Access Specifier class

Private Yes No No

Protected Yes Yes No

Public Yes Yes Yes

10
Book References
• Php: The Complete Reference, Steven Holzner, Tata McGraw-
Hill Education, 2007
• Modern PHP: New Features and Good Practices, Josh Lockhart,
"O'Reilly Media, Inc.“, 2015.
• PHP for the Web: Visual QuickStart Guide, Larry Ullman
Pearson Education, 2006.
• https://www.tutorialspoint.com/php/index.htm
• https://www.codecademy.com/learn/learn-php

11
THANK YOU

You might also like