:: How to use

Source: Internet
Author: User
Tags access properties getcolor

<?php
/*
*php Double colon:: (scope-scoped operator) usage
* You can access properties and methods overridden in static, const, and class.
* Within the class ontology, you can use the "self:: constant name" or ""
* "Class Name:: Constant name" can be used in vitro
* In PHP 5.3.0, you can use variables instead of class names. However, the value of the variable cannot be the keyword self, parent, or static.
* Generally define defines constants outside of the class, const defines constants within the class but php5.3 the constants by const beyond the supported classes
The difference between *define and const:
* 1.const cannot define constants in conditional statements, but define is possible
* 2.const uses an ordinary constant name, define can use an expression as the name
* 3.const can only accept static scalars, and define may use any expression.
*/
Static Properties, Methods:
1, does not need to instantiate can directly with self:: static method Name "or" "Class can use" class Name:: Static method Name "call;
2, static properties, methods a public no matter how many times, static methods are loaded directly into the memory of a fixed space, in memory only one copy, for all the instances shared; Common method your method call needs to instantiate the class first and then this operation will open up another space in memory to hold this part of the space is also omitted ~ It's upstairs. Provincial Resources
3, static properties and methods, can only access static properties and methods, cannot class access non-static properties and methods. Because static properties and methods are created, there may not be any instances of this class that can be called.
4. Static methods cannot invoke non-static properties. Because the non-static attribute needs to be instantiated, it is stored in the object;
5, static methods can call non-static methods, using the Self keyword. In PHP, a method is self: after that, it automatically transforms into a static method;
1 Constants Define external access (invoke) in a class
Class Person
{
Const Const_value = constant value of the ' person class; ';
}
Assigning a class name to a variable
$classname = ' person ';
In PHP 5.3.0, you can use variables instead of class names.
echo "called with variable name instead of class name". $classname:: Const_value;
External Call constants
echo "External call". Person::const_value;
echo "<br/>";
External use variable name instead of class name to call constant value of the person class
External invoke constant value of the person class
echo "==============================================================================<br/>";
Class Person2
{
Const Const_value = constant value of the ' Person2 class; ';
1 Constants Define internal access (invoke) in a class

Public Function demofunction ()
{
Self::const_value;
}
}
$a =new person2 ();
$a->demofunction ();
echo $a:: Const_value. " \ n "; After PHP 5.3.0
The constant value of the Person2 class;


echo "<pre>=================== uses ==========================================<pre> outside the class definition";
Class Person3
{
Const Const_value = constant value of the ' Person3 class ';
}
Class Boy3 extends Person3
{

public static $name 3 = ' Lichihua ';//static property
public static function first ()//static method
{
echo Parent::const_value. ";"; /Call a constant of the parent class
echo Self:: $name 3. ";"; /Invoke properties of the class itself
}
}
Boy3::first ();
The constant value of the Person3 class;
Lichihua;


echo "<pre>=================== calls the method ==========================================<pre> of the parent";
Class Person4
{
protected function Eat4 ()
{
echo "Person::eat () \ n;";
}
}
Class Boy4 extends Person4
{


Public Function Eat4 ()
{
PARENT::EAT4 ();
echo "boy::eat;";
}
}
$b = new Boy4 ();
$b->eat4 ();
Person::eat ();
Boy::eat ()


echo "<br/>=================== uses the scope qualifier???? ==========================================<pre> ";
Generally used to use the Class A object's properties/Methods in Class B objects!
Class Apple
{
Public Function Showcolor ()
{
return $this->color;
}
}

Class Banana
{
Public $color;

Public Function __construct ()
{
$this->color = "Banana is yellow";
}

Public Function GetColor ()
{
return Apple::showcolor ();
}
}

$banana = new Banana;
echo $banana->getcolor ();
Banana is yellow

echo "<br/>=================== method to call the base class???? ==========================================<pre> ";
Class fruit2{
static function color () {
return "COLOR";
}
static function Showcolor () {
echo "Output color (show)". Self::color ();
}
}

Class Apple2 extends Fruit2
{
static function color () {
return "Red";
}
}

Apple2::showcolor ();
Output colors (show) color

?>

=============================================================================================================== ===========

<?php
Class MyClass
{
Const Const_value = ' MyClass constant value '. <br/> ";


protected function MyFunc () {
echo "method in MyClass <br/>";
}
}
There are two ways to call class constants,
FA 1/////////////////////////////////////////////////////////////
$classname = ' MyClass '; ////
echo $classname:: Const_value; From PHP 5.3.0////
//// ////
//////////////////////////////////////////////////////////////
Method 2
Echo Myclass::const_value;

Class Otherclass extends MyClass
{
public static $my _static = ' static property value of the Otherclass class ';

public static function Doublecolon () {
echo "Otherclass class calls the constant of the parent class (MyClass):". Parent::const_value. "\ n";
echo Self:: $my _static. "<br/>";
}
}

$classname = ' Otherclass ';
echo $classname::d Oublecolon (); From PHP 5.3.0

Otherclass::d Oublecolon ();


When a subclass overrides a method in its parent class, PHP does not invoke methods that have been overridden in the parent class. Whether the method of calling the parent class depends on the child class. This mechanism is also used for constructors and destructors, overloading, and magic methods.
Class OtherClass2 extends MyClass
{
Overrides the definition of the parent class
Public Function MyFunc ()
{
But you can still call the overridden method in the parent class
echo "Quilt class overridden by the parent class method if the subclass is read with the same instance as the Parent:: Method name or can be invoked here output:";
Parent::myfunc ();
echo "method in OtherClass2, where the subclass overrides the method of the parent class, if the line does not appear (the method in the MyClass) This means that there is no call to the overridden method <br/>";
}
}

$class = new OtherClass2 ();
$class->myfunc ();//The subclass here is called externally, so you must instantiate and invoke the method to output


Class constants, class properties (static??). and class functions (static) can share the same name and use double colons to access such
Class A
{
public static $B = "1";//static property of class
Const B = "2";//Class constant
public static function B () {
Return "3";
}//static function of class
Why does static function use return??
}

echo A:: $B. A::B.A::B (). " <br/> ";


Class CA
{
Directly Set property default value
protected static $item = ' Foo ';
To set default property values indirectly
protected static $other = ' CA ';

public static function method ()
{
Print self:: $item. " \ r \ n "; Print Foo in a variety of ways
Print self:: $other. " \ r \ n "; We think this print CA but.
}

public static function Setother ($val)
{
Self:: $other = $val; Set a value in this range.
}
}

Class CB extends CA
{
/**
* Try to define a default value for the re-attribute
*/
protected static $item = ' Bar ';

public static function Setother ($val)
{
Self:: $other = $val;
}
}

Cb::setother (' CB '); Setother subclass the Parent class has a method that calls the subclass CB but there is no output because the subclass overrides the method of the parent class that is the Setother method of the subclass and the contents of the subclass method do not have the echo output so there is no output
If it is Ca::method (), the output is foo and CA;
Cb::method (); Output Foo and CB Subclass call the parent class of the method! subclass itself without method methods (subclass inheritance is a method property that can be called in the parent class) Gaga just tried it. As long as the parent class and subclass two lei as long as a Setother method can output CB do not know what the principle

Class CC extends CA
{
/**
* Redefine default values for attributes
*/
protected static $item = ' Tango ';

public static function method ()
{
Print self:: $item. " \ r \ n "; Print Foo in a variety of ways
Print self:: $other. " \ r \ n "; We think this print CA but
}

/**
* Now we'll redefine the Setother () method, using CA "Self::" Just for fun.
*/
}
Cc::setother (' CC '); There should be no Setother method in CC so call here is the method of the parent class
Cc::method (); Here the subclass overrides the parent class, and the parent class method is equivalent to not being called!

/*
Attention
If you only call Cc::setother (' CC ') then it will not output because the parent class's Setother method does not have the echo output, plus it can output CC
If only call Cc::method () then output Tango and CB
If Cc::setother (' cc ') and Cc::method () are present here, Tango and CC will appear and Cc::setother (' CC ') must be called before Cc::method () or the tango and CB will be output.

*/

Class CD extends CA
{
/**
* Redefine attribute default values
*/
protected static $item = ' Foxtrot ';


/**
* Now we're going to redefine that there's a way to do the problem.
*/
}
Cd::setother (' CD '); This is called the Setother method of the CA parent class! Or the same as the above no output!
Cd::method (); This is called the method of the CA parent class ()! output foo and CD can tell if the redefined $item value in the CD is not in effect or if the output foo wants to output foxtrot then you must define method methods in the CD subclass to override the parent class method.

/*

Get random string
function generaterandomstring ($length = 4) {
$characters = ' 0123456789abcdefghijklmnopqrstuvwxyz ';
$randomString = ";
for ($i = 0; $i < $length; $i + +) {
$randomString. = $characters [rand (0, strlen ($characters)-1)];
}
return $randomString;
}
Echo generaterandomstring ();
echo "<pre>";
*/

=============================================================================================================== ===============

Class A {
Const C = ' Consta ';
Public Function m () {
Echo static::c; Use the range value of ' static '
}
Public Function n () {
Echo self::c;
}
}

Class B extends A {
Const C = ' CONSTB ';

}

$b = new B ();
$b->m (); Output CONSTB that the output is a constant of subclasses
$b->n ();//output Consta The output is a constant of the parent class

:: How to use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.