对类的摸索~~俺用了半年时间才大概理解类的作用和实现。主要是没有一篇能让我理解的文章(之前没接触过任何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 了。到底哪个是哪个啊~~

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

WebStorm Mac version
Useful JavaScript development tools

mPDF
mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.