PHP functions What are Functions? A function structure: <?php function myname( $1arg, $2arg,….. $narg) { echo "Example function.\n"; return $retval; }

Slides:



Advertisements
Similar presentations
Classes & Objects INTRODUCTION : This chapter introduces classes ; explains data hiding, abstraction & encapsulation and shows how a class implements these.
Advertisements

Final and Abstract Classes
Object Oriented Programming in PHP 5
Java Inheritance. What is inherited A subclass inherits variables and methods from its superclass and all of its ancestors. The subclass can use these.
1 Composition A whole-part relationship (e.g. Dog-Tail) Whole and part objects have same lifetime –Whole creates instance of part in its constructor In.
Evan Korth New York University Computer Science I Classes and Objects Professor: Evan Korth New York University.
June 1, 2000 Object Oriented Programming in Java (95-707) Java Language Basics 1 Lecture 3 Object Oriented Programming in Java Language Basics Classes,
1 Chapter 6 Inheritance, Interfaces, and Abstract Classes.
Chapter 13: Overloading.
©2004 Brooks/Cole Chapter 10 More on Classes. Figures ©2004 Brooks/Cole CS 119: Intro to JavaFall 2005 Object assignment With primitive types, setting.
Shallow Versus Deep Copy and Pointers Shallow copy: when two or more pointers of the same types point to the same memory – They point to the same data.
Lecture From Chapter 6 & /8/10 1 Method of Classes.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
Introduction to Object Oriented Programming. Object Oriented Programming Technique used to develop programs revolving around the real world entities In.
Internet Software Development Classes and Inheritance Paul J Krause.
Programming in Java Unit 2. Class and variable declaration A class is best thought of as a template from which objects are created. You can create many.
APCS Java AB 2004 Review of CS1 and CS2 Review for AP test #1 Sources: 2003 Workshop notes from Chris Nevison (Colgate University) AP Study Guide to go.
OOP IN PHP `Object Oriented Programming in any language is the use of objects to represent functional parts of an application and real life entities. For.
Tuc Goodwin  Object and Component-Oriented Programming  Classes in C#  Scope and Accessibility  Methods and Properties  Nested.
Web Database Programming Week 3 PHP (2). Functions Group related statements together to serve “a” specific purpose –Avoid duplicated code –Easy maintenance.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 7 Structured Data and Classes.
1 Chapter 8 – Classes and Object: A Deeper Look Outline 1 Introduction 2 Implementing a Time Abstract Data Type with a Class 3 Class Scope 4 Controlling.
ADTs and C++ Classes Classes and Members Constructors The header file and the implementation file Classes and Parameters Operator Overloading.
OOP: Encapsulation,Abstraction & Polymorphism. What is Encapsulation Described as a protective barrier that prevents the code and data being randomly.
Inheritance Chapter 10 Programs built from objects/instances of classes An O.O. approach – build on earlier work. Use classes in library and ones you have.
 Classes in c++ Presentation Topic  A collection of objects with same properties and functions is known as class. A class is used to define the characteristics.
Inheritance, Polymorphism, And Virtual Functions Chapter 15.
8. Inheritance “Is-a” Relationship. Topics Creating Subclasses Overriding Methods Class Hierarchies Abstract Class Inheritance and GUIs The Timer Class.
Lecture 10 Concepts of Programming Languages Arne Kutzner Hanyang University / Seoul Korea.
 Constructor  Finalize() method  this keyword  Method Overloading  Constructor Overloading  Object As an Argument  Returning Objects.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
1 Chapter 5: Defining Classes. 2 Basics of Classes An object is a member of a class type What is a class? Fields & Methods Types of variables: –Instance:
CSci 162 Lecture 10 Martin van Bommel. Procedures vs Objects Procedural Programming –Centered on the procedures or actions that take place in a program.
CS-1030 Dr. Mark L. Hornick 1 Basic C++ State the difference between a function/class declaration and a function/class definition. Explain the purpose.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 14: More About Classes.
Inheritance Objectives: Creating new classes from existing classes The protected modifier Creating class hierarchies Abstract classes Indirect visibility.
Access Specifier. Anything declared public can be accessed from anywhere. Anything declared private cannot be seen outside of its class. When a member.
21. PHP Classes To define a class, use the keyword class followed by the name and a block with the properties and method definitions Properties are declared.
OOP Basics Classes & Methods (c) IDMS/SQL News
Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 6-1 Learning Objectives  Classes  Constructors  Principles of OOP  Class type member.
C++ General Characteristics: - Mixed typing system - Constructors and destructors - Elaborate access controls to class entities.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 10 Java Fundamentals Objects/ClassesMethods.
JAVA ACCESS MODIFIERS. Access Modifiers Access modifiers control which classes may use a feature. A classes features are: - The class itself - Its member.
Unit 2. Constructors It initializes an object when it is created. It has same as its class and syntactically similar to a method. Constructor have no.
Introduction to C++ programming Recap- session 1 Structure of C++ program Keywords Operators – Arithmetic – Relational – Logical Data types Classes and.
Classes CS 162 (Summer 2009). Parts of a Class Instance Fields Methods.
Geoff Holmes and Bernhard Pfahringer COMP206-08S General Programming 2.
OOP: Encapsulation &Abstraction
Classes C++ representation of an object
PHP Classes and Objects
Using local variable without initialization is an error.
Object Oriented Programming
CS240: Advanced Programming Concepts
Overloading and Overriding
Simple Classes in C# CSCI 293 September 12, 2005.
Inheritance Dr. Bhargavi Goswami Department of Computer Science
Classes & Objects: Examples
Interfaces.
Java Inheritance.
Object-Oriented Programming in PHP
Chapter 8 Classes User-Defined Classes and ADTs
Tonga Institute of Higher Education
Method of Classes Chapter 7, page 155 Lecture /4/6.
CIS 199 Final Review.
Classes C++ representation of an object
Final and Abstract Classes
Classes and Objects Imran Rashid CTO at ManiWeber Technologies.
Lecture 10 Concepts of Programming Languages
Haidong Xue Summer 2011, at GSU
SPL – PS3 C++ Classes.
Presentation transcript:

PHP functions What are Functions? A function structure: <?php function myname( $1arg, $2arg,….. $narg) { echo "Example function.\n"; return $retval; } ?>

Eg For Globals <?php $a = 1; $b = 2; function Sum() { global $a, $b; $b = $a + $b; } Sum(); echo $b; ?>

Eg For Statics <?php function Tables() { static $count = 0; static $x=0; $y=2; $count++; $x= $x + $y; echo $x ; If ($count < 10) { Tables(); } } ?>

Conditional Functions <? function even() { echo "its an even number "; } function odd() { echo"its an odd number "; } $even=false; If ($even == TRUE) even(); Else odd(); ?>

Nested Functions <? function fname() { echo "fname "; function lname() { echo "lname "; } //lname(); not accesible fname(); lname(); ?> Here lname() can only be called after calling fname()

Function Arguments Passing By Value Eg: <? function add($num) { $sum=$num[0]+$num[1]; echo "$sum "; } $num = array(0,1); add($num); echo $num[0]; ?>

Passing By Reference <?php function name(&$string) { $string.= 'lname.'; } $str = 'fname '; name($str); echo $str; // outputs fname lname ?>

Use Of Default Parameters Ex: <? function comname ($lname="Softech") { return "company name is $lname "; } echo comname(); echo comname("Relyon");//prints Relyon ?>

Continued <? function comname ($fname, $lname="Softech") { return "company name is $fname $lname"; } echo comname("Relyon");//prints Relyon Softech ?>

Variable Length Argument List and Variable Functions <? function add($a,$b) { $numargs=func_num_args(); if($numargs>=2) print("the second arg is ".func_get_arg(1)." \n"); print("the no of arguments passed to this function are $numargs"); echo" "; $arg_list=func_get_args();

Continued for($i=0;$i<$numargs;$i++) { echo "Argument $i is: ". $arg_list[$i]. " \n"; } $c=$a+$b; return $c; } $d='add'; $e=$d(2,10); echo " sum=$e "; ?>

CLASSES A class is a collection of variables and functions which work on this variables. A simple class declaration: Class first{ //member variables public $a = 1; //method declaration public function display() { echo this->a;//member methods; }

New New is used to create an instance of a class, a new object must be created and assigned to a variable. <? Class name { public $a=1; public function display() { echo $this->a; } $b = new name; $b->display(); ?>

Extends and Constructors <?php class parentclass { // member declaration public $name = 'Relyon'; // method declaration public function display() { echo $this->name; }

Continued class childclass extends parentclass { function display() { echo "Extending company\n"; parent::display(); } $extended = new childclass(); $extended->display(); ?>

Final It prevents child classes from overriding a method by prefixing the definition with final. If the class itself is being defined final then it cannot be extended. Ex: <?class fname { final public function name() { echo relyon ;} class lname extends fname { public function name() { echo softech ;} } ?>

Visibility Private Protected public

Ex For Visibility <? class firstone { public $public = "public"; protected $protected = "protected"; private $private = "private"; private function privateone() { echo "hi "; } protected function protectedone() { echo"protected "; } $ob = new firstone; $ob->public;

continued //$ob->protected; //$ob->private; //$ob->privateone(); class secondone extends firstone { public function hello() { $this->protected; $this->protectedone(); } $c=new secondone; $c->hello(); ?>

Scope Resolution Operator(::) <? class name { const name1 = 'relyon '; } echo name::name1; class sname extends name { public static $sname = "ltd"; public static function display() { echo parent::name1; echo self::$sname; } sname::display(); ?>

Abstract Classes <?php abstract class name { // Force Extending class to define this method abstract protected function getname(); abstract protected function prefixname($prefix); // Common method public function display() { print $this->getname(). "\n"; }

Continued class fname1 extends name { public function getname() { return "Relyon"; } public function prefixname($prefix) { return "$prefix"; } $class1 = new fname1; $class1->display(); echo $class1->prefixname('softech')."\n"; ?>