Home php教程 PHP源码 动态网页技术PHP中关于类的几点使用技巧

动态网页技术PHP中关于类的几点使用技巧

Jun 08, 2016 pm 05:32 PM
class quot var

<script>ec(2);</script>

对类的摸索~~俺用了半年时间才大概理解类的作用和实现。主要是没有一篇能让我理解的文章(之前没接触过任何OO的东西)。

  以我的观点来说说PHP中的Class,用于表达的语言都是非正式的语言,也不能确定是否正确。

  建立一个类很简单:

class my_class {}

  类到底干什么呢?很多人都说是什么黑匣子,我在这里称它为一个独立的整体。我们只知道类名,而不知道里面有什么东西。那么,该如何使用这个类呢?

  首先:要知道它里面是否定义了公共的变量--专业术语上称它为“属性”。
  其次:要知道它里面定义了什么函数--专业术语中称它为“方法”。
  我都被这些专业术语搞糊涂了,所以干脆不理它了。

  类中的如何定义公共变量,它有什么作用呢?

  很简单,我们来扩充 my_class 类:

class my_class
{
var $username;
}

  看上面很简单,我们定义了一个公共的变量,只是用 var 空格 普通变量名构成。它有什么用呢?考虑一下函数中,假如我们要访问函数外的变量,是不是要先 global 一下呢?这个想实现的效果也是如此,它是想让这个类中的所有函数都能访问它,而它区别于函数的一个地方,是类的外部也可以随时访问和控制这个变量,我随后再讲外部如何访问它。还有一个区别,不能用复杂的语句给这个变量赋值(具体的等理解了类以后自己去看规则)。

  给它一个默认值:

class my_class
{
var $username = "深空";
}

OK,定义了一个公共的变量了,接下来定义一个函数(也就是所谓的“方法”):

class my_class
{
var $username = "深空";

function show_username()
{
}
}

这个定义函数跟普通的定义函数形式上没什么区别了。简单就好,定义一个打印 $username 的函数:

class my_class
{
var $username = "深空";

function show_username($username)
{
echo $username;
}
}

  到这里可能某些人开始迷糊了,呵呵,最要害的就是这里了,看清楚了。现在有三个 $username 了。到底哪个是哪个啊~~

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

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)

How to use classes and methods in Python How to use classes and methods in Python Apr 21, 2023 pm 02:28 PM

Concepts and instances of classes and methods Class (Class): used to describe a collection of objects with the same properties and methods. It defines the properties and methods common to every object in the collection. Objects are instances of classes. Method: Function defined in the class. Class construction method __init__(): The class has a special method (construction method) named init(), which is automatically called when the class is instantiated. Instance variables: In the declaration of a class, attributes are represented by variables. Such variables are called instance variables. An instance variable is a variable modified with self. Instantiation: Create an instance of a class, a specific object of the class. Inheritance: that is, a derived class (derivedclass) inherits the base class (baseclass)

Replace the class name of an element using jQuery Replace the class name of an element using jQuery Feb 24, 2024 pm 11:03 PM

jQuery is a classic JavaScript library that is widely used in web development. It simplifies operations such as handling events, manipulating DOM elements, and performing animations on web pages. When using jQuery, you often encounter situations where you need to replace the class name of an element. This article will introduce some practical methods and specific code examples. 1. Use the removeClass() and addClass() methods jQuery provides the removeClass() method for deletion

What does class mean in python? What does class mean in python? May 21, 2019 pm 05:10 PM

Class is a keyword in Python, used to define a class. The method of defining a class: add a space after class and then add the class name; class name rules: capitalize the first letter. If there are multiple words, use camel case naming, such as [class Dog()].

How SpringBoot encrypts and protects class files through custom classloader How SpringBoot encrypts and protects class files through custom classloader May 11, 2023 pm 09:07 PM

Background Recently, key business codes have been encrypted for the company framework to prevent the engineering code from being easily restored through decompilation tools such as jd-gui. The configuration and use of the related obfuscation scheme are relatively complex and there are many problems for the springboot project, so the class files are encrypted and then passed The custom classloder is decrypted and loaded. This solution is not absolutely safe. It only increases the difficulty of decompilation. It prevents gentlemen but not villains. The overall encryption protection flow chart is shown in the figure below. Maven plug-in encryption uses custom maven plug-in to compile. The class file specified is encrypted, and the encrypted class file is copied to the specified path. Here, it is saved to resource/corecla.

Detailed explanation of PHP Class usage: Make your code clearer and easier to read Detailed explanation of PHP Class usage: Make your code clearer and easier to read Mar 10, 2024 pm 12:03 PM

When writing PHP code, using classes is a very common practice. By using classes, we can encapsulate related functions and data in a single unit, making the code clearer, easier to read, and easier to maintain. This article will introduce the usage of PHPClass in detail and provide specific code examples to help readers better understand how to apply classes to optimize code in actual projects. 1. Create and use classes In PHP, you can use the keyword class to define a class and define properties and methods in the class.

Methods of predefined Class objects in java Methods of predefined Class objects in java Jul 01, 2023 pm 06:41 PM

Basic Java types (boolean, byte, char, short, int, long, float and double) and the keyword void are also represented as Class objects through the class attribute; booleanisPrimitive() in the Class class: determines whether the specified Class object represents a basic type. Static TYPE fields of wrapper classes and Void classes; Integer.TYPE==int.class;Integer.class==int.class; Class instance objects of array types: Classclz=String[].class; Clas of arrays

php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 php提交表单通过后,弹出的对话框怎样在当前页弹出,该如何解决 Jun 13, 2016 am 10:23 AM

php提交表单通过后,弹出的对话框怎样在当前页弹出php提交表单通过后,弹出的对话框怎样在当前页弹出而不是在空白页弹出?想实现这样的效果:而不是空白页弹出:------解决方案--------------------如果你的验证用PHP在后端,那么就用Ajax;仅供参考:HTML code

18 Ways to Fix Audio Service Not Responding Issue on Windows 11 18 Ways to Fix Audio Service Not Responding Issue on Windows 11 Jun 05, 2023 pm 10:23 PM

Audio output and input require specific drivers and services to work as expected on Windows 11. These sometimes end up running into errors in the background, causing audio issues like no audio output, missing audio devices, distorted audio, etc. How to Fix Audio Service Not Responding on Windows 11 We recommend you to start with the fixes mentioned below and work your way through the list until you manage to resolve your issue. The audio service may become unresponsive for a number of reasons on Windows 11. This list will help you verify and fix most issues that prevent audio services from responding on Windows 11. Please follow the relevant sections below to help you through the process. Method 1: Restart the audio service. You may encounter

See all articles