PHP Code: what exactly does the
class my_class {}
class do?
class is a collection of variables and scopes. The variable in the
class is the attribute
define a class class dd{
var $user=.quot; the yma.Quot; / / / / attribute
}
}
$name= new DD;
$name-.gt; and many people are what black boxes, I call it an independent whole. We only know class names, but we don't know what's in them. So, how do you use this class?
First: we need to know if there is a public variable in it, which is called "attribute" in technical terms.
next: we need to know what functions are defined in it, which is called "method" in technical terms.
I was confused by these technical terms, so I simply ignored it. How does
class define public variables? What does it do? It's very simple. Let's extend the my_class class of
PHP Code:
class my_class
{
var $username;
}
}
it's very simple. We define a common variable, only with the ordinary variable name of the space lattice. What's the use of it? Consider the function. If we want to access variables outside the function, do we need to global first? The same is true of this. It wants to have all the functions in the class access it, but it is different from one place in the function, the outside of the class can also be accessed, and I then talk about how to access it outside. There is another difference. It is impossible to assign variables to complex variables by using complex statements. Give it a default value
PHP Code:
class my_class
{
var $username =.Quot; deep empty.Quot;;
PHP Code:
class my_class
{
var $username =.Quot; deep space.Quot; It's easy to define a function to print $username:
PHP Code:
class my_class
{
var $username =.Quot; The key is here, and see it. Now there are three $username. In the end, which one is the ~ (
) function, do you need to explain it? This function function is to print the value received by the row parameter, that is, if
PHP Code:
show_username (.Quot; pig head deep.Quot;);
it will print "pig head deep", that is so simple. How do
how to access this function? Definitely not what I said directly show_username (.Quot; pig head deep space.Quot;); don't worry, there is a class like set. As follows: the PHP Code:
$Name = new my_class ();
that initializes the class of the my_class above and assigns the object to the variable $Name, and you can understand that this variable represents the whole class, huh.
use the function in the class:
PHP Code:
$Name-.gt; show_username (.Quot; pig head deep empty.Quot;);
halo, why is this complex? And the arrow? In fact, it's very image. I have already given the class to the variable $Name, right? That is, $Name represents this class, and then uses an arrow to point to the show_username function in the class. It's so simple, that is, this function is in this class, rather than other functions -- you understand it as a difference, huh.
try it. Oh, print the four words "pig head deep space". Why do you say it's so complicated? Is it possible to use a function, too? I said, of course, you can't see the benefits of such a simple thing. We will continue to expand.
there is another question: why does the "public variable" just mentioned have no use at all? Why does this function do not automatically accept the default values in this common variable var $username? That is if I use:
PHP Code:
$Name-.gt; show_username ($username);
what will the result be? The answer is no output. Because you don't give $username a value to the parameter.
How do we use this public variable? Let's modify this class:
PHP Code:
class my_class
{
var $username =.Quot; There's another $this-.gt; it's dizzy. In fact, this is also one of the biggest conveniences of the class. The role of
$this: to access a common variable, or a function in a class.
access? So professional? In fact, it uses $this-.gt; username instead of VaR $username; $this is used to show that it is public. Something that can be accessed, outside the function.
see:
PHP Code:
$Name-.gt; show_username ();
see, finally print "deep empty" these two words, Wahaha.
I do not print the two words "deep space". I want to print "pig head deep space". What should I do? Very simple, we assign the value to this public variable. I took you.
PHP Code:
$Name-.gt; username =.Quot; pig head deep space.Quot;
can this understand? $Name-.gt; username represents the common variable in the class. The assignment of the equivalent number is not to be explained by me.
print again to see
PHP Code:
$Name-.gt; show_username ();
ha ha, finally printed "pig head deep space". Not bad. It's very convenient. You can modify the printing value without any parameter.
simply printing a name is too boring. Let's say a bit of welcome, to expand this class, to create a function called Welcome:
PHP Code:
class my_class
{
var $username =; $this-.gt; username;
}
function Welcome ()
{
}
}
}
grace, what functions do you want to achieve?
PHP Code:
class my_class
{
var $username =.Quot; deep empty.Quot; Greet.Quot;;
$this-.gt; show_username ();
}
}
see $this for the second time? $this-.gt; show_username (); what does it use to do? In fact, it is called the function of show_username, using $this to indicate that the function is in the class and is parallel to the Welcome function, not in other places (such as the Welcome function). The function of the
Welcome function is simple, first printing two words.Quot; welcome.Quot; then then execute the show_username function and print the name.
to try this function:
PHP Code:
$Name-.gt; Welcome ();
see, print out "welcome deep empty" these four words.
but I want to print "welcome pig head deep space". What should I do? I took you, let's give the public variable var $username a value: the
PHP Code:
$Name-.gt; username =.Quot; the pig head deep empty.Quot;
to print the welcome. What about
? Do you understand the use of the class? The advantage is that you can call any function in the class, and you can change the value of a common variable by using $this, and you can use this public variable in the function in the class... The application is waiting for you to find out. The
php construction method, when the object is produced, the first call initializes the member. If PHP4 and PHP5 construction methods are called together, PHP4 does not execute the construction method, and the PHP5 construction method directly executes the PHP5 construction method of
PHP4. Er{
}
}
php5 __construct
class user{
is the property or method inside the object inside its own object, inside the object can be made Use or access, the extension of the subclass of the package member
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
class, extends
class, which cannot be used in the external or other objects of the object, is an extension to the original class; < br />
[code].lt;?
class namee{
function Namee ($name)
{
$this-.gt; user=$name;
}
> >; {
var $school;
function study ()
{
}
}
[code].lt;?
class student
{
var $school; / / / / defines a property
function definitions () / > >$q=new student; / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / >
$q-.gt; school=.quot; Li four.Quot;
$q-.gt; study ();
.Gt; N show_user () {/ / / / > definition method
echo $this-.gt; name=.quot; HA.Quot; / / / / / / / / / >
}
}
inheritance parent / > R / > {
parent:: show_user (); / / / / / / called parent method
echo.Quot; Hello ha.Quot;
}
}
; ~~~~~~~~~~~~~~~~
protected can only be used inside the class, including subclasses. It can not be used outside the class.
protected function clas () {}