Martin Kruliš 26. 2. 2015 by Martin Kruliš (v1.0)1.

Slides:



Advertisements
Similar presentations
PHP I.
Advertisements

Object Oriented Programming in PHP 5
Object-Oriented programming in C++ Classes as units of encapsulation Information Hiding Inheritance polymorphism and dynamic dispatching Storage management.
Constructor. 2 constructor The main use of constructors is to initialize objects. A constructor is a special member function, whose name is same as class.
Programming Languages and Paradigms
Chapter 9 Pointers and Dynamic Arrays. Overview 9.1 Pointers 9.2 Dynamic Arrays.
A C LOSER L OOK AT C LASSES 1. A SSIGNING O BJECTS One object can be assigned to another provided that both objects are of the same type. It is not sufficient.
Copyright © 2012 Pearson Education, Inc. Chapter 9 Delegates and Events.
Subprograms A subprogram allows process abstraction (as opposed to data abstraction). Characteristics –single entry point –caller suspended until control.
1 Classes, Encapsulation, Methods and Constructors (Continued) Class definitions Instance data Encapsulation and Java modifiers Method declaration and.
Chapter 7: User-Defined Functions II
ECE 353: Lab C Pointers and Structs. Basics A pointer holds an address to some variable Notation: – Dereferencing operator: * int *x is a declaration.
Chapter 14: Overloading and Templates
Lifetime “The lifetime of a variable is the time during which the variable is bound to a specific memory location.” [p. 219] “…the lifetime of a variable.
1 Names, Scopes and Bindings. 2 Names Kinds of names Kinds of names Variables, functions, classes, types, labels, blocks, operators, tasks, etc. Variables,
Using Templates Object-Oriented Programming Using C++ Second Edition 11.
More Classes in C++ Bryce Boe 2012/08/20 CS32, Summer 2012 B.
Review of C++ Programming Part II Sheng-Fang Huang.
Java Methods By J. W. Rider. Java Methods Modularity Declaring methods –Header, signature, prototype Static Void Local variables –this Return Reentrancy.
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look.
“is a”  Define a new class DerivedClass which extends BaseClass class BaseClass { // class contents } class DerivedClass : BaseClass { // class.
NMED 3850 A Advanced Online Design January 26, 2010 V. Mahadevan.
Programmer's view on Computer Architecture by Istvan Haller.
Martin Kruliš This is an Object Oriented system. If we change something, the users object by Martin Kruliš (v1.0)1.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
OOP with PHP Roman Bednarik
 2006 Pearson Education, Inc. All rights reserved Classes: A Deeper Look, Part 2.
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.
CS212: Object Oriented Analysis and Design Lecture 7: Arrays, Pointers and Dynamic Memory Allocation.
Data Structures Using C++ 2E Chapter 3 Pointers. Data Structures Using C++ 2E2 Objectives Learn about the pointer data type and pointer variables Explore.
Martin Kruliš by Martin Kruliš (v1.0)1.
Data TypestMyn1 Data Types The type of a variable is not set by the programmer; rather, it is decided at runtime by PHP depending on the context in which.
Martin Kruliš by Martin Kruliš (v1.0)1.
COMP3190: Principle of Programming Languages
CPSC 252 The Big Three Page 1 The “Big Three” Every class that has data members pointing to dynamically allocated memory must implement these three methods:
Object-Based Programming Mostly Review. Objects Review what is object? class? member variables? member functions? public members? private members? friend.
Module 8: Delegates and Events. Overview Delegates Multicast Delegates Events When to Use Delegates, Events, and Interfaces.
1 Using Templates COSC 1567 C++ Programming Lecture 10.
COP INTERMEDIATE JAVA Designing Classes. Class Template or blueprint for creating objects. Their definition includes the list of properties (fields)
Chapter 11 – Pointer Variables. Declaring a Pointer Variable u Declared with data type, * and identifier type* pointer_variable; u * follows data type.
Martin Kruliš by Martin Kruliš (v1.1)1.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 15: Overloading and Templates.
Functions Illustration of: Pass by value, reference Scope Allocation Reference: See your CS115/215 textbook.
From C++ to C# Part 5. Enums Similar to C++ Similar to C++ Read up section 1.10 of Spec. Read up section 1.10 of Spec.
Unit 10-JavaScript Functions Instructor: Brent Presley.
Fall 2004CSI University of Ottawa Introduction to PHP Basic principles and syntax.
 Variables can store data of different types, and different data types can do different things.  PHP supports the following data types:  String  Integer.
PHP Reusing Code and Writing Functions 1. Function = a self-contained module of code that: Declares a calling interface – prototype! Performs some task.
Chapter 1 C++ Basics Review (Section 1.4). Classes Defines the organization of a data user-defined type. Members can be  Data  Functions/Methods Information.
Methods main concepts – method call – object 's methods – actual parameters – method declaration – formal parameters – return value other concepts – method.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 7 – Pointers.
Reference Types CSE301 University of Sunderland Harry R Erwin, PhD.
Martin Kruliš by Martin Kruliš (v1.1)1.
Lambda Functions & Closures A Sydney PHP Group Presentation 2 nd October 2008 By Timothy Chandler.
PHP Internals Martin Kruliš by Martin Kruliš (v1.2)
Friend Class Friend Class A friend class can access private and protected members of other class in which it is declared as friend. It is sometimes useful.
Advance OOP in PHP.
Object Oriented PHP Martin Kruliš
Programming with ANSI C ++
Constructors & Destructors.
Class: Special Topics Copy Constructors Static members Friends this
Methods The real power of an object-oriented programming language takes place when you start to manipulate objects. A method defines an action that allows.
Subroutines Web Programming.
Classes, Encapsulation, Methods and Constructors (Continued)
PHP – Language Details Martin Kruliš by Martin Kruliš (v1.0)
Object-Oriented Programming in PHP
PHP Language Revision of Fundamentals
The C++ programming language
A Deeper Look at Classes
Threads and concurrency / Safety
Presentation transcript:

Martin Kruliš by Martin Kruliš (v1.0)1

 Interpreted Language ◦ No pointers ◦ Dynamic data structures usually depend on pointers  Instead of Pointers ◦ The arrays are flexible enough ◦ Objects are passed by reference (not by value)  What Else Is There ◦ Variable variables ◦ References by Martin Kruliš (v1.0)2

 Indirect Access to Values ◦ Name of one variable is stored in another variable $a = 'b'; $$a = 42; // the same as $b = 42; $a = 'b'; $b = 'c'; $c = 'd'; $$$$a = 'hello'; // the same as $d = 'hello'; ◦ The {, } can be used to avoid ambiguous situations ◦ Can be used with members, functions, classes, … $obj = new $className(); $obj->$varName = 42; by Martin Kruliš (v1.0)3

 References ◦ Similar to Unix hard-links in FS ◦ Multiple variables attached to the same data ◦ Independent on object references  A reference to an object can be created $a = 1; $b = &$a; $b++; echo $a; // prints by Martin Kruliš (v1.0)4 int (1) $a $b int (2)

 Arguments as References ◦ Similar usage as var keyword in Pascal function inc(&$x) { $x++; }  Returning References function &findIt($what) { global $myArray; return &$myArray[$what]; } by Martin Kruliš (v1.0)5

 References vs. Pointers function foo(&$var) { $var = &$GLOBALS['bar']; }  The unset() Function ◦ Does not remove data, only the variable ◦ Data are removed when not referenced  The global Declaration global $a;  $a = &$GLOBALS['a']; by Martin Kruliš (v1.0)6

 Declaration ◦ Keyword function followed by the identifier function foo([args, …]) { … body … }  Function Body ◦ Pretty much anything (even a function/class decl.)  Returning Results ◦ Optional argument of the return construct ◦ Only one value, but it can be an array by Martin Kruliš (v1.0)7

 Argument Declarations ◦ Implicit values may be provided function foo($x, $y = 1, $z = 2) { … }  Arguments with implicit values are aligned to the right  Note that PHP functions does not support overloading  Variable Number of Arguments ◦ Any function can be called with more arguments than formally declared ◦ Functions func_num_args(), func_get_arg(), and func_get_args() provide access to such arguments by Martin Kruliš (v1.0)8

 Indirect Calling ◦ Calling a function by its name stored in a variable function foo($x, $y) { … } $funcName = 'foo'; $funcName(42, 54);// the same as foo(42, 54);  Similar Constructs ◦ Using specialized invocation functions  call_user_func('foo', 42, 54);  call_user_func_array('foo', array(42, 54)); by Martin Kruliš (v1.0)9

 Testing Function Existence ◦ function_exists() – test whether given func. exists ◦ get_defined_functions() – list of all func. names  Cleanup Functions ◦ register_shutdown_function() – registers a function, which is executed when the script finishes  Special Case of Left-side Function ◦ Func. list() is used at the left-side of assignments  Reverse logic – it fills its arguments list($a, $b, $c) = array(1, 2, 3); by Martin Kruliš (v1.0)10

 Lambda (Nameless) Functions ◦ A unique name is generated automatically ◦ Function create_function()  Gets the arguments and the body as strings  Returns an identifier of newly created function ◦ Useful in many situations  One-call functions  Call-back functions $mul = create_function('$x, $y', 'return $x * $y'); echo $mul(3, 4);// prints out '12' by Martin Kruliš (v1.0)11

 Anonymous Functions ◦ New way how to implement nameless functions $fnc = function($args) { …body… }; ◦ The anonymous function is an instance of Closure  It can be passed on like an object ◦ The visible variables must be explicitly stated $fnc = function(…) use ($var, &$refvar) { … };  These variables are captured in the closure  Variables passed by reference can be modified by Martin Kruliš (v1.0)12 Example 1

by Martin Kruliš (v1.0)13