Learn PHP


php class


Concept for Object Oriented Design


Name Description
Object-Oriented With Object-Oriented Programming, we can model the general idea by using a class.
class A class is a unit of code that describes the characteristics and behaviors of something.
object An object is a specific instance of a class.
properties The characteristics of a class or object are known as its properties.
method The behaviors(actions) of a class are known as its methods.

PHP Class Definition


In PHP we use class keyword to define a class.


	      	class Car { 
			   //properties
			   //methods
			}   
	      

PHP Create Object from Class


A class is defining or creating a new type. We can create variables for the new type. We call it object in object oriented programming.


	      	$aRectangleObject = new RectangleClass;
	      

	      	<?PHP
			class Shape {
			   public function say() {
			      print "shape";
			   }
			}

			$aRectangle = new Shape;
			$aRectangle->say();
			?>
	      


Objects Within Objects


You can use objects inside other objects. Using -> to access objects within objects.


		  	<?PHP
			class NameTag {
			        public $Words;
			}
			class Book {
			   public $Name;
			   public $NameTag;
			   public function say() {
			      print "Book";
			   }
			}
			$aBook = new Book;
			$aBook->Name = "PHP";
			$aBook->NameTag = new NameTag;
			$aBook->NameTag->Words = "from studentsempire.com";
			?>
		  

PHP Class Properties


Class properties are very similar to variables.


An object's property can store a single value, an array of values, or even another object.


To add a property to a class, first write the keyword public, private, or protected, followed by the property's name preceded by a $symbol.


public, private, or protected are the visibility levels you want to give to the property:


		  	class MyClass { 
			    public $property1;     // This is a public property 
			    private $property2;    // This is a private property 
			    protected $property3;  // This is a protected property 
			}   
		  

Accessing Properties


We can access the corresponding object's property value from within your calling code by using the following syntax: $object->property;


Write the name of the variable storing the object, followed by an arrow symbol composed of a hyphen (-) and a greater - than symbol (>), followed by the property name.


Note that the property name doesn't have a $ symbol before it.


Example


		  		<?PHP
				class Shape{
				   public $Name;

				   public function say() {
				       print "shape";
				   }
				}
				$aRectangle = new Shape;
				$aRectangle->say();
				$aRectangle->Name = "Rect";
				$aRectangle->say();
				?>
		  

Each object has its own set of properties. Consider the following code:


		  		<?PHP
				class Shape{
				   public $Name;

				   public function say() {
				       print "shape";
				   }
				}
				class Rectangle extends Shape{
				}


				$aRect = new Rectangle;
				$bRect = new Rectangle;
				$aRect->Name = "A";
				$bRect->Name = "B";
				print $aRect->Name;
				print $bRect->Name;
				?>
		  


PHP allows you to dynamically declare new properties for objects. It would create the property only for the object, and not for any other instances of the same class


PHP Iterating Object Properties


We can treat an object as an array with the foreach loop. foreach will iterate over each of the properties that are accessible.


That is, private and protected properties will not be accessible in the general scope.


		  	<?PHP
			class Person {
			        public $FirstName = "Parth";
			        public $MiddleName = "Raj";
			        public $LastName = "Vivek";
			        private $Password = "pass";
			        public $Age = 25;
			        public $HomeTown = "IN";
			}

			$bill = new Person();

			foreach($bill as $var => $value) {
			        echo "$var is $value\n";
			}
			?>
		  

If the foreach loop is called inside a method, we should be able to see the property:


		  	<?PHP
			class Person {
			        public $FirstName = "Parth";
			        public $MiddleName = "Raj";
			        public $LastName = "Vivek";
			        private $Password = "pass";
			        public $Age = 25;
			        public $HomeTown = "IN";
			        public function outputVars() {
			                foreach($this as $var => $value) {
			                       echo "$var is $value\n";
			                }
			        }
			 }

			 $bill = new Person();
			 $bill->outputVars();
			?> 
		  




PHP Tutorials


Home

Introduction

Syntax

Variable

Comment

Echo and Print Statement

Data Types

Boolean

Integer

Number Convert

PHP String

php variable substitution in string

php string element

php string function

php addcslashes function

php addslashes function

php get magic quotes gpc function

php get meta tags function

php urlencode function

PHP $ and $$ Variables

PHP constant

PHP Magic Constants

PHP Static Variable

PHP Super Globals

PHP Globals

PHP Server

PHP reserved variable get

PHP reserved variable post

PHP reserved variable FILES

PHP reserved variable Cookies

PHP reserved variable Session

PHP reserved variable Request

PHP reserved variable environment

PHP Operator

PHP Arithmetic Operator

PHP Comparison Operator

PHP Incrementing and Decrementing Operators

PHP Identical Operators

PHP Logical Operators

PHP String Operators

PHP Ternary Operators

PHP Execution Operators

PHP Operator Precedence

PHP Array Operator

PHP Class

PHP Class Inheritance

PHP constructor and destructor

PHP abstract class

PHP class access control

PHP statement

PHP If statement

PHP else statement

PHP else if statement

PHP control structure statement

PHP while loop

PHP do while loop

PHP for loop

PHP for each loop

PHP break statement

PHP continue statement

PHP declare statement

PHP return statement

PHP require statement

PHP include

PHP require once

PHP include once

PHP goto statement

PHP function

PHP function parameter

PHP function scope

PHP array

PHP array operator

PHP array element loop

PHP array foreach loop

PHP array multidimensional

PHP html forms

PHP get and post method

form redirect

file inclusion

file i/o