Home > Backend Development > PHP Tutorial > An introductory Class article_PHP tutorial

An introductory Class article_PHP tutorial

WBOY
Release: 2016-07-13 16:58:17
Original
809 people have browsed it

I just took a quick look at the article about Class that was updated on the homepage (referring to the PHPE article http://www.phpe.net/articles/389.shtml). It’s very good. I recommend reading it.
Exploring classes~~ It took me half a year to roughly understand the functions and implementation of classes. The main reason is that there is no article that I can understand (I have never been exposed to any OO stuff before).
From my point of view, the language used to express Class in PHP is informal, and I am not sure whether it is correct.
Creating a class is easy.
PHP code:-------------------------------------------------- ------------------------------------
class my_class {}
---- -------------------------------------------------- --------------------------
What exactly do classes do? What many people are is a black box, I call it an independent whole here. We only know the class name, but not what is inside. So, how to use this class?
First of all: you need to know whether there are public variables defined in it - called "properties" in professional terms.
Secondly: You need to know what function is defined in it - it is called a "method" in professional terms.
I was confused by all the jargon, so I just ignored it.
How to define public variables in a class and what does it do?
It’s very simple, let’s extend the my_class class
PHP code:--------------------------------- --------------------------------------------------
class my_class
{
var $username;
}
-------------------------------- --------------------------------------------------
Looking at the above, it is very simple. We have defined a public variable, which is just composed of var + space + ordinary variable name. What is it used for? Consider a function. If we want to access variables outside the function, do we need to make it global first? The same is true for this effect. It wants all functions in this class to be able to access it, and one thing that distinguishes it from functions is that this variable can also be accessed from outside the class. I will talk about how to access it from the outside later. . There is another difference. You cannot use complex statements to assign a value to this variable (see the rules for yourself after you understand the class). Give it a default value
PHP code:--------------------------------------------- ----------------------------------------
class my_class
{
var $username = "Deep Space";
}
---------------------------------- --------------------------------------------------
OK, a public variable has been defined. Next, define a function (the so-called "method").
PHP code:-------------------------------------------------- ------------------------------------

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631424.htmlTechArticleI just took a quick look at the article about Class that was updated on the homepage (referring to the PHPE article http:/ /www.phpe.net/articles/389.shtml), it’s very good, I recommend you take a look. Exploration of categories~~ I...
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