Home Backend Development PHP Tutorial About loading classes in PHP

About loading classes in PHP

Mar 01, 2021 pm 03:50 PM
kind object-oriented

Object-oriented is an important idea, and classes are also important concepts in object-oriented, but class loading is the key to using classes.

There are two ways to access classes:

  • Access through instantiated objects

  • Access to class members

The prerequisite for access is that there is a class in the memory, so the class needs to be loaded into the memory in advance.

1. Manual loading

1

2

3

4

5

6

7

8

//类文件 Salary.php

<?php

   class Salary{

      public  function Student(){

        echo "Salary下面的Student方法";

      }

   }

?>

Copy after login

1

2

3

4

5

6

7

8

应用文件:useSalary.php

<?php

//$s = new Salary();会报错,因为useSalaty.php中间未定义Salary类

 

include_once &#39;Salary.php&#39;;              //也可以使用require,通常使用_once,因为类不允许重名

$s = new Salary();

echo $s->Student();

?>

Copy after login

1

2

3

4

5

6

7

8

9

<?php

//加载类文件是一种比较消耗资源的方式,可以事先使用class_exists()函数来判定类是否存在,存在就不用加载,不存在才加载

if(!class_exists(&#39;Salary&#39;)){

    //不存在:加载

    include_once &#39;Salary.php&#39;;

}

//使用

$s = new Salary();

?>

Copy after login

2. Automatic loading

  • The automatic loading mechanism used before PHP7: Use the __autoload() function provided by the system. Then when the system needs to use the class and it does not exist in the memory, the system will automatically call the __autoload() function to load the class file. .

1

2

3

4

5

6

7

8

9

<?php

function __autoload($classname){    //参数为类名:即当前需要访问的类的名字

//需要人为定义去哪加载,怎么加载  

    include_once $classname . &#39;.php&#39;;   //假定为当前目录下,类文件名字为:类名.php

}

 

//使用类:内存目前并没有

$s = new Salary();  //系统发现内存没有Salary,所以调用__autoload()去加载

?>

Copy after login

1

2

3

4

5

6

7

8

9

10

//若在不同路径下

<?php

//定义自动加载

function __autoload($classname){

    $abc_file = &#39;abc/&#39; . $classname . &#39;.php&#39;;       //如abc/Salary.php

    if(file_exists($c_file)){                      //利用file_exists()判断文件是否存在

        include_once $abc_file;

    }

}

?>

Copy after login
  • After PHP7, it is not recommended to use the __autoload() function, but to use a registration mechanism to customize the user The function is placed inside the system and uses spl_autoload_register (defined function).

1

2

3

4

5

6

7

8

9

10

<?php

function myself_autoload($classname){       //与__autoload()类似

    $abc_file = &#39;abc/&#39; . $classname . &#39;.php&#39;;       //如abc/Salary.php

    if(file_exists($c_file)){

        include_once $c_file;

    }

}

//此时,上述函数永远不会自动运行,除非将函数注册到系统内部

spl_autoload_register(&#39;myself_autoload&#39;);

?>

Copy after login

1

2

3

4

5

6

7

8

9

10

11

//可以定义多个方法

<?php

function wayone_autoload($classname){      

    

function waytwo_autoload($classname){      

     

}

//此时,上述函数永远不会自动运行,除非将函数注册到系统内部

spl_autoload_register(&#39;wayone_autoload&#39;);

spl_autoload_register(&#39;waytwo_autoload&#39;);

?>

Copy after login

Recommended: php tutorial

The above is the detailed content of About loading classes in PHP. For more information, please follow other related articles on the PHP Chinese website!

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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the importance of @JsonIdentityInfo annotation using Jackson in Java? What is the importance of @JsonIdentityInfo annotation using Jackson in Java? Sep 23, 2023 am 09:37 AM

The @JsonIdentityInfo annotation is used when an object has a parent-child relationship in the Jackson library. The @JsonIdentityInfo annotation is used to indicate object identity during serialization and deserialization. ObjectIdGenerators.PropertyGenerator is an abstract placeholder class used to represent situations where the object identifier to be used comes from a POJO property. Syntax@Target(value={ANNOTATION_TYPE,TYPE,FIELD,METHOD,PARAMETER})@Retention(value=RUNTIME)public

Explore object-oriented programming in Go Explore object-oriented programming in Go Apr 04, 2024 am 10:39 AM

Go language supports object-oriented programming through type definition and method association. It does not support traditional inheritance, but is implemented through composition. Interfaces provide consistency between types and allow abstract methods to be defined. Practical cases show how to use OOP to manage customer information, including creating, obtaining, updating and deleting customer operations.

PHP Advanced Features: Best Practices in Object-Oriented Programming PHP Advanced Features: Best Practices in Object-Oriented Programming Jun 05, 2024 pm 09:39 PM

OOP best practices in PHP include naming conventions, interfaces and abstract classes, inheritance and polymorphism, and dependency injection. Practical cases include: using warehouse mode to manage data and using strategy mode to implement sorting.

Packaging technology and application in PHP Packaging technology and application in PHP Oct 12, 2023 pm 01:43 PM

Encapsulation technology and application encapsulation in PHP is an important concept in object-oriented programming. It refers to encapsulating data and operations on data together in order to provide a unified access interface to external programs. In PHP, encapsulation can be achieved through access control modifiers and class definitions. This article will introduce encapsulation technology in PHP and its application scenarios, and provide some specific code examples. 1. Encapsulated access control modifiers In PHP, encapsulation is mainly achieved through access control modifiers. PHP provides three access control modifiers,

Analysis of object-oriented features of Go language Analysis of object-oriented features of Go language Apr 04, 2024 am 11:18 AM

The Go language supports object-oriented programming, defining objects through structs, defining methods using pointer receivers, and implementing polymorphism through interfaces. The object-oriented features provide code reuse, maintainability and encapsulation in the Go language, but there are also limitations such as the lack of traditional concepts of classes and inheritance and method signature casts.

Are there any class-like object-oriented features in Golang? Are there any class-like object-oriented features in Golang? Mar 19, 2024 pm 02:51 PM

There is no concept of a class in the traditional sense in Golang (Go language), but it provides a data type called a structure, through which object-oriented features similar to classes can be achieved. In this article, we'll explain how to use structures to implement object-oriented features and provide concrete code examples. Definition and use of structures First, let's take a look at the definition and use of structures. In Golang, structures can be defined through the type keyword and then used where needed. Structures can contain attributes

'Introduction to Object-Oriented Programming in PHP: From Concept to Practice' 'Introduction to Object-Oriented Programming in PHP: From Concept to Practice' Feb 25, 2024 pm 09:04 PM

What is object-oriented programming? Object-oriented programming (OOP) is a programming paradigm that abstracts real-world entities into classes and uses objects to represent these entities. Classes define the properties and behavior of objects, and objects instantiate classes. The main advantage of OOP is that it makes code easier to understand, maintain and reuse. Basic Concepts of OOP The main concepts of OOP include classes, objects, properties and methods. A class is the blueprint of an object, which defines its properties and behavior. An object is an instance of a class and has all the properties and behaviors of the class. Properties are characteristics of an object that can store data. Methods are functions of an object that can operate on the object's data. Advantages of OOP The main advantages of OOP include: Reusability: OOP can make the code more

In-depth understanding of PHP object-oriented programming: Debugging techniques for object-oriented programming In-depth understanding of PHP object-oriented programming: Debugging techniques for object-oriented programming Jun 05, 2024 pm 08:50 PM

By mastering tracking object status, setting breakpoints, tracking exceptions and utilizing the xdebug extension, you can effectively debug PHP object-oriented programming code. 1. Track object status: Use var_dump() and print_r() to view object attributes and method values. 2. Set a breakpoint: Set a breakpoint in the development environment, and the debugger will pause when execution reaches the breakpoint, making it easier to check the object status. 3. Trace exceptions: Use try-catch blocks and getTraceAsString() to get the stack trace and message when the exception occurs. 4. Use the debugger: The xdebug_var_dump() function can inspect the contents of variables during code execution.

See all articles