Detailed explanation of usage examples of PHP namespace and automatic loading classes

伊谢尔伦
Release: 2023-03-12 06:56:01
Original
1164 people have browsed it

This article mainly introduces PHP object-orientednamespace and automatic loading class of programming, and analyzes php namespace and automatic loading in combination with examples. The concept, function, usage and related Notes of loading classes, friends in need can refer to the following

This article describes the namespace and automatic loading classes of PHP object-oriented programming with examples. Share it with everyone for your reference, the details are as follows:

Namespace

Avoid duplication of class names and cause errors.

<?php
require_once "useful/Outputter.php";
class Outputter {
  // output data
  private $name;
  public function setName($name) {
    $this->name = $name;
  }
  public function getName() {
    return $this->name;
  }
}
$obj = new Outputter(); // 同一命名空间下,类名不能相同,默认命名空间为空。空也是一种命名空间。
$obj -> setName("Jack");
print $obj->getName();
//namespace useful; // 更改命名空间,否则查询不到Hello类,Fatal error: Class &#39;my\Hello&#39; not found
$hello = new Hello();
?>
<?php
// useful/Outputter.php
namespace useful; // 命名空间
class Outputter {
  //
}
class Hello {
}
?>
Copy after login

How to call classes in the namespace

<?php
namespace com\getinstance\util;
class Debug {
  static function helloWorld() {
    print "hello from Debug\n";
  }
}
namespace main;
// com\getinstance\util\Debug::helloWorld(); // 找不到Debug类
\com\getinstance\util\Debug::helloWorld(); // 加斜杠之后,就从根部去寻找了。
// outPut:hello from Debug
?>
Copy after login

Use the use keyword

<?php
namespace com\getinstance\util;
class Debug {
  static function helloWorld() {
    print "hello from Debug\n";
  }
}
namespace main;
use com\getinstance\util;
//Debug::helloWorld(); //Fatal error: Class &#39;main\Debug&#39; not found
util\Debug::helloWorld();
?>
Copy after login

Use the following Processing, you can directly call the class

<?php
namespace com\getinstance\util;
class Debug {
  static function helloWorld() {
    print "hello from Debug\n";
  }
}
namespace main;
use com\getinstance\util\Debug; // 直接使用到类
Debug::helloWorld();
?>
Copy after login

\ to represent the global

global.php

<?php
// no namespace
class Lister {
  public static function helloWorld() {
    print "hello from global\n";
  }
}
?>
<?php
namespace com\getinstance\util;
require_once &#39;global.php&#39;;
class Lister {
  public static function helloWorld() {
    print "hello from ".NAMESPACE."\n"; // NAMESPACE当前namespace
  }
}
Lister::helloWorld(); // access local
\Lister::helloWorld(); // access global
?>
Copy after login

Output:

hello from com\ getinstance\util
hello from global

namespace plus {}

<?php
namespace com\getinstance\util {
  class Debug {
    static function helloWorld() {
      print "hello from Debug\n";
    }
  }
}
namespace main {
  \com\getinstance\util\Debug::helloWorld();
}
?>
Copy after login

output:

hello from Debug

Global namespace

<?php
namespace { // 全局空间
  class Lister {
    public static function helloWorld() {
      print "hello from global\n";
    }
  }
}
namespace com\getinstance\util {
  class Lister {
    public static function helloWorld() {
      print "hello from ".NAMESPACE."\n";
    }
  }
  Lister::helloWorld(); // access local
  \Lister::helloWorld(); // access global
}
?>
Copy after login

autoload automatic loading class

ShopProduct.php

<?php
class ShopProduct {
  function construct() {
    print "ShopProduct constructor\n";
  }
}
?>
<?php
function autoload( $classname ) { // 自动加载,根据类名加载类
  include_once( "$classname.php" );
}
$product = new ShopProduct( &#39;The Darkening&#39;, &#39;Harry&#39;, &#39;Hunter&#39;, 12.99 );
?>
Copy after login

output:

ShopProduct constructor

Further optimization processing

is located in the folder business/ShopProduct.php

<?php
class business_ShopProduct { // 这里的类命名就要遵循规则了
  function construct() {
    print "business_ShopProduct constructor\n";
  }
}
?>
<?php
function autoload( $classname ) {
  $path = str_replace(&#39;_&#39;, DIRECTORY_SEPARATOR, $classname ); // 智能化处理
  require_once( "$path.php" );
}
$x = new ShopProduct();
$y = new business_ShopProduct();
?>
Copy after login

output:

ShopProduct constructor
business_ShopProduct constructor

The above is the detailed content of Detailed explanation of usage examples of PHP namespace and automatic loading classes. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!