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

Namespaces in The Wild2

This document discusses namespaces in PHP 5.3. Namespaces help organize code, reduce conflicts, and shorten names. Namespaces encapsulate items and were designed to solve name collisions and allow aliasing of long names. The document also discusses how and when to use namespaces and provides examples and demos.

Uploaded by

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

Namespaces in The Wild2

This document discusses namespaces in PHP 5.3. Namespaces help organize code, reduce conflicts, and shorten names. Namespaces encapsulate items and were designed to solve name collisions and allow aliasing of long names. The document also discusses how and when to use namespaces and provides examples and demos.

Uploaded by

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

All rights reserved. Zend Technologies, Inc.

\Namespaces \in \the \Wild


with PHP 5.3
By Stas Malyshev and Michael Spector
All rights reserved. Zend Technologies, Inc.
Why have namespaces?
Organize OO code into self-contained units
Reduce conflicts
Shorten names
class
Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum_CaseInsensitive
extends Zend_Search_Lucene_Analysis_Analyzer_Common_TextNum
Isolate and abstract functions, classes, etc.
All rights reserved. Zend Technologies, Inc.
What are Namespaces?
In the broadest definition namespaces are a way of
encapsulating items.
designed to solve two problems when creating re-usable code:
Name collisions between code you create, and internal PHP
classes/functions/constants or third-party
classes/functions/constants.
Ability to alias (or shorten) Extra_Long_Names designed to
alleviate the first problem, improving readability of source code.
3 Insert->Header & Footer
Java C++ PHP
package illustration;
import java.awt.*;
public class Drawing {
...
}
using namespace std;
namespace first
{
int var = 5;
}
namespace my\name;
use your\name;
class MyClass {}
function myfunct (){}
consnt MYCONST = 1;
All rights reserved. Zend Technologies, Inc.
Namespaces Properties
Compile-time definition
Mostly compile-time resolution
Many NS per file, many files per NS
All rights reserved. Zend Technologies, Inc.
5 Insert->Header & Footer
<?php
namespace my\name; // see "Defining Namespaces" section
use My\Full\Classname as Another;
class MyClass {}
function myfunction() {}
const MYCONST = 1;
$a = new MyClass;
$obj = new Another;
$c = new \my\name\MyClass; // see "Global Space" section
$a = strlen('hi'); // see "Using namespaces: fallback to global
// function/constant" section
$d = namespace\MYCONST; // see "namespace operator and __NAMESPACE__
// constant" section
$d = __NAMESPACE__ . '\MYCONST';
echo constant($d); // see "Namespaces and dynamic language features" section
?>
First look at Namespaces
All rights reserved. Zend Technologies, Inc.
Introducing namespaces
define(MY_HTTP_GET, 1);
define(MY_HTTP_POST, 2);
class My_Http_Request {
function __construct(
$method =
ZEND_HTTP_GET)
{
}
}
function my_http_send_request(
My_Http_Request $request) {
}
namespace My\Http;
const GET = 1;
const PUT = 2;
class Request {
function __construct(
$method = GET)
{
}
}
function send_request(
Request $request) {
}
All rights reserved. Zend Technologies, Inc.
Should I use Namespaces?
Yes! if your application is based on a framework or component
that exposes namespaces
Probably! if you write a PHP 5.3 framework or component
that will be used by others
Probably not! if you write PHP components that are unlikely
to be embedded/reused by others, or if you use PHP for short
scripting or for fun!
No! if you use legacy code or other cases that namespaces are
not required by code structure
7 Insert->Header & Footer
All rights reserved. Zend Technologies, Inc.
Demo #1 Simple usage
|
8
All rights reserved. Zend Technologies, Inc.
Demo #2 Debugging
|
9
All rights reserved. Zend Technologies, Inc.
Supporting Products
Zend Server Community Edition
Including PHP 5.3
Zend Studio and Eclipse PDT 2.1
New syntax highlighter
Support new language features in PHP
5.3, such as namespaces and closures
Navigation and Exploration of
namespaces
Code assist based on new type
inference
Hyperlinks to namespaces or classes
PHP 5.3 debugging information
10 Insert->Header & Footer
All rights reserved. Zend Technologies, Inc.
Recap and takeaways
Namespaces are here to solve name collisions and to
shorten elements names
How to use (or not-use) namespaces
There are many new things in PHP 5.3 that you should read
about!
11 Insert->Header & Footer
All rights reserved. Zend Technologies, Inc.
Q&A

You might also like