PHP Namespaces (in progress)

Slides:



Advertisements
Similar presentations
Introduction to PHP Dr. Charles Severance
Advertisements

PHP Functions / Modularity
JavaScript Functions, Objects, Array, and Control Structures Dr. Charles Severance
Expressions and Control Flow in PHP Dr. Charles Severance
PHP Object Oriented Programming (OOP) Dr. Charles Severance
Getting PHP Hosting Dr. Charles Severance. What We Need We want to be able to put up our application on the web so anyone can access our URL (and so we.
Forms and PHP Dr. Charles Severance
Cookies, Sessions, and Authentication Dr. Charles Severance
Win8 on Intel Programming Course Modern UI HelloWorld in HTML5/JS Cédric Andreolli Intel.
Go to your school’s web locker site school name.schoolweblockers.com) Your user name is the first letter of your first name, the first four.
PHP Arrays Dr. Charles Severance
Go to your school’s web locker site Your user name is the first letter of your first name, the first four letters of.
Variables, Expressions, and Statements
Conditional Execution Chapter 3 Python for Informatics: Exploring Information
Introduction to Dynamic Web Content Dr. Charles Severance
1 Project 5: Printing Address Labels. 2 Assignment Write a Windows forms program to display and print a set of address labels. Input from a csv file.
This is Where the Title of Your Presentation Goes Presented by Your name Design ©2014 Social Media Examiner Do NOT distribute.
PHP Functions Dr. Charles Severance
Forms and PHP Dr. Charles Severance
PHP Arrays Dr. Charles Severance
PHP Functions / Modularity Dr. Charles Severance
Loops and Iteration Chapter 5 Python for Informatics: Exploring Information
1 Using jQuery JavaScript & jQuery the missing manual (Second Edition)
Introduction to Dynamic Web Content Dr. Charles Severance
Retrieving and Visualizing Data Charles Severance Python for Everybody
Dr. Charles Severance Using Handlebars Dr. Charles Severance
C.R.U.D. Charles Severance
Introduction to Dynamic Web Content
PHP Arrays Dr. Charles Severance
Our Technologies Dr. Charles Severance
Reading Files Chapter 7 Python for Everybody
Python Dictionaries Chapter 9 Python for Everybody
HTML Charles Severance
PHP Functions / Modularity
Transactions Dr. Charles Severance
Cascading Style Sheets
Getting PHP Hosting Dr. Charles Severance
Using JSON Dr. Charles Severance
Conditional Execution
PHP Object Oriented Programming (OOP)
Redirect, Routing, and Authentication
Regular Expressions Chapter 11 Python for Everybody
Introduction to PHP Dr. Charles Severance
Relational Databases and MySQL
2 8 thousands hundreds tens ones
Loops and Iteration Chapter 5 Python for Everybody
Introduction to PHP Dr. Charles Severance
HTTP Parameters and Arrays
Functions Chapter 4 Python for Everybody
PHP Arrays Dr. Charles Severance
Object Oriented Programming in JavaScript
Expressions and Control Flow in PHP
Tuples Chapter 10 Python for Everybody
ISC440: Web Programming 2 Server-side Scripting PHP 3
Introduction to Dynamic Web Content
Using jQuery Dr. Charles Severance
This document is released under Creative Commons License Attribution 4.0 International. Please feel free to share and adapt this document with.
Charles Severance Single Table SQL.
Introduction to Dynamic Web Content
1 2 3 This document is released under Creative Commons License Attribution 4.0 International. Please feel free to share and adapt this document with appropriate.
Insert Presentation Title Here Insert Presentation Summary Here
Guess that number ? ? This document is released under Creative Commons License Attribution 4.0 International. Please feel free to share and adapt this.
Choose This document is released under Creative Commons License Attribution 4.0 International. Please feel free to share and adapt this document with appropriate.
Guess that number ? ? This document is released under Creative Commons License Attribution 4.0 International. Please feel free to share and adapt this.
This document is released under Creative Commons License Attribution 4
HTML Tags and Structure
Data Modelling Many to Many
1 2 This document is released under Creative Commons License Attribution 4.0 International. Please feel free to share and adapt this document with appropriate.
Model View Controller (MVC)
Presentation transcript:

PHP Namespaces (in progress) Dr. Charles Severance www.wa4e.com

Namespaces and Frameworks

PHP has many "Frameworks" There are many PHP Frameworks with large code bases Each framework cannot define a global function called "setup" or they would overwrite each other We need a naming convention to keep these large code bases from stepping on each other so that applications can use more than one framework

www.php-fig.org

PHP-FIG Standards What to name classes and where to place them in the source directory http://www.php-fig.org/psr/psr-0/ How to properly namespace classes to avoid collission http://www.php-fig.org/psr/psr-1/

OO In Frameworks OO allows us to avoid clashes for global function names $date = DateTime::createFromFormat('j-M-Y', '15-Feb-2009'); We also want to avoid clashes on class names as well

Name Spaces The PHP namespace directive lets us "silo" all of our classes in a file within a global "namespace" tsugi/lib/vendor/Tsugi/Core/User.php <?php namespace Tsugi\Core; class User { ... $d = new \DateTime('today +1 week') } Fully-qualified class name: \Tsugi\Core\User

Accessing a Class in a Namespace To get the class we want – we must indicate which the name space we want <?php ... $USER = new \Tsugi\Core\User(); <?php use \Tsugi\Core\User; $USER = new User(); tsugi/lib/vendor/Tsugi/Core/LTIX.php

Auto Loader for Classes Normally we put class definition in files and then include them do the classes are defined for our code <?php ... require_once("vendor/Tsugi/Core/User.php"); require_once("vendor/Tsugi/Core/Context.php"); require_once("vendor/Tsugi/Core/Link.php"); require_once("vendor/Tsugi/Core/LTIX.php"); tsugi/lib/lms_lib.php

Auto Loader for Classes With large PHP frameworks like Symfony, we may have many hundreds of classes that are used very rarely PHP has the ability to auto-load classes when code tries to "new" a class that is not yet loaded But standards are needed because it is best if there is one autoloader across all frameworks http://www.php-fig.org/psr/psr-4/ http://api.symfony.com/

Folder and Class Naming So that PHP auto loaders can be interoperable, there are rules about class names and folder names and you are only supposed to put one class in a file \Tsugi\Core\User tsugi/lib/vendor/Tsugi/Core/User.php \Tsugi\Core\Context tsugi/lib/vendor/Tsugi/Core/Context.php \Tsugi\Core\Settings tsugi/lib/vendor/Tsugi/Core/Settings.php http://www.php-fig.org/psr/psr-4/

Summary There is a trend away from global class names and toward namespaces and namespace conventions

Acknowledgements / Contributions Continue new Contributors and Translators here These slides are Copyright 2010- Charles R. Severance (www.dr-chuck.com) as part of www.wa4e.com and made available under a Creative Commons Attribution 4.0 License. Please maintain this last slide in all copies of the document to comply with the attribution requirements of the license. If you make a change, feel free to add your name and organization to the list of contributors on this page as you republish the materials. Initial Development: Charles Severance, University of Michigan School of Information Insert new Contributors and Translators here including names and dates Note from Chuck. Please retain and maintain this page as you remix and republish these materials. Please add any of your own improvements or contributions.