Home Backend Development PHP Tutorial 小弟我觉得好迷糊

小弟我觉得好迷糊

Jun 13, 2016 pm 01:23 PM
method protected quot static

我觉得好迷糊啊

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

--><?php class cA
{
    /**
     * Test property for using direct default value
     */
    protected static $item = 'Foo';
    
    /**
     * Test property for using indirect default value
     */
    protected static $other = 'cA';
    
    public static function method()
    {
        print self::$item."\r\n"; // It prints 'Foo' on everyway... :(
        print self::$other."\r\n"; // We just think that, this one prints 'cA' only, but... :)
    }
    
    public static function setOther($val)
    {
        self::$other = $val; // Set a value in this scope.
    }
}

class cB extends cA
{
    /**
     * Test property with redefined default value
     */
    protected static $item = 'Bar';
    
    public static function setOther($val)
    {
        self::$other = $val;
    }
}

class cC extends cA
{
    /**
     * Test property with redefined default value
     */
    protected static $item = 'Tango';
    
    public static function method()
    {
        print self::$item."\r\n"; // It prints 'Foo' on everyway... :(
        print self::$other."\r\n"; // We just think that, this one prints 'cA' only, but... :)
    }
    
    /**
     * Now we drop redeclaring the setOther() method, use cA with 'self::' just for fun.
     */
}

class cD extends cA
{
    /**
     * Test property with redefined default value
     */
    protected static $item = 'Foxtrot';
    
    /**
     * Now we drop redeclaring all methods to complete this issue.
     */
}

cB::setOther('cB'); // It's cB::method()!
cB::method(); // It's cA::method()!
cC::setOther('cC'); // It's cA::method()!
cC::method(); // It's cC::method()!
cD::setOther('cD'); // It's cA::method()!
cD::method(); // It's cA::method()!

/**
 * Results: ->
 * Foo
 * cB
 * Tango
 * cC
 * Foo
 * cD
 * 
 * What the hell?! :)
 */

?>
Copy after login



这是覆盖,还是什么啊?为什么输出这样啊,不能理解啊,听乱的啊。

------解决方案--------------------
这叫什么呢?自找麻烦!
为累而累
------解决方案--------------------
不理解就算了,无所谓的事。
------解决方案--------------------
探讨

这叫什么呢?煮饺子找麻烦!
为累而累

------解决方案--------------------
这样可能会清楚些
PHP code
class cA
{
    /**
     * Test property for using direct default value
     * 使用直接默认值测试属性
     */
    protected static $item = 'Foo';
    
    /**
     * Test property for using indirect default value
     * 使用间接默认值测试属性
     */
    protected static $other = 'cA';
    
    public static function method()
    {
        print __METHOD__ . ' ' . __CLASS__ . '::$item=' . self::$item."\r\n";
        print __METHOD__ . ' ' . __CLASS__ . '::$otfer=' . self::$other."\r\n";
    }
    
    public static function setOther($val)
    {
        self::$other = $val; // Set a value in this scope.
    }
}

class cB extends cA
{
    /**
     * Test property with redefined default value
     * 重新定义了默认值测试属性
     */
    protected static $item = 'Bar';
    
    public static function setOther($val)
    {
        self::$other = $val;
    }
}

class cC extends cA
{
    /**
     * Test property with redefined default value
     * 重新定义了默认值测试属性
     */
    protected static $item = 'Tango';
    
    public static function method()
    {
        print __METHOD__ . ' ' . __CLASS__ . '::$item=' . self::$item."\r\n";
        print __METHOD__ . ' ' . __CLASS__ . '::$otfer=' . self::$other."\r\n";
    }
    
    /**
     * Now we drop redeclaring the setOther() method, use cA with 'self::' just for fun.
     */
}

class cD extends cA
{
    /**
     * Test property with redefined default value
     * 重新定义了默认值测试属性
     */
    protected static $item = 'Foxtrot';
    
    /**
     * Now we drop redeclaring all methods to complete this issue.
     * 现在,我们放弃重新声明的所有方法来完成这个问题
     */
}

cB::setOther('cB'); // It's cB::method()!
cB::method(); // It's cA::method()!
cC::setOther('cC'); // It's cA::method()!
cC::method(); // It's cC::method()!
cD::setOther('cD'); // It's cA::method()!
cD::method(); // It's cA::method()!
 <div class="clear">
                 
              
              
        
            </div>
Copy after login
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)

Solution to PHP Fatal error: Call to a member function fetch() Solution to PHP Fatal error: Call to a member function fetch() Jun 23, 2023 am 09:36 AM

When using PHP for web application development, you will often need to use a database. When using a database, error messages are very common. Among them, PHPFatalerror: Calltoamemberfunctionfetch() is a relatively common error that occurs when using PDO to query the database. So, what causes this error and how to solve it? This article will explain it in detail for you. 1. Cause of error

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

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

How to use static, this, super, and final in Java How to use static, this, super, and final in Java Apr 18, 2023 pm 03:40 PM

1. static Please look at the following program first: publicclassHello{publicstaticvoidmain(String[]args){//(1)System.out.println("Hello, world!");//(2)}} Have seen this Segment programs are familiar to most people who have studied Java. Even if you have not learned Java but have learned other high-level languages, such as C, you should be able to understand the meaning of this code. It simply outputs "Hello, world" and has no other use. However, it shows the main purpose of the static keyword.

What is the function and usage of static in C language What is the function and usage of static in C language Jan 31, 2024 pm 01:59 PM

The role and usage of static in C language: 1. Variable scope; 2. Life cycle; 3. Internal function; 4. Modify global variables; 5. Modify function; 6. Other uses; Detailed introduction: 1. Variable scope, when If there is the static keyword before a variable, then the scope of the variable is limited to the file in which it is declared. In other words, the variable is a "file-level scope", which is very useful for preventing the "duplicate definition" problem of variables; 2. Life cycle, static variables are initialized once when the program starts executing, and destroyed when the program ends, etc.

The role of static The role of static Jan 24, 2024 pm 04:08 PM

The functions of static: 1. Variables; 2. Methods; 3. Classes; 4. Other uses; 5. Multi-threaded environment; 6. Performance optimization; 7. Singleton mode; 8. Constants; 9. Local variables; 10. Memory Layout optimization; 11. Avoid repeated initialization; 12. Use in functions. Detailed introduction: 1. Variables, static variables. When a variable is declared as static, it belongs to the class level, not the instance level, which means that no matter how many objects are created, only one static variable exists, and all objects share this Static variables and so on.

In Java, can we declare a top-level class as protected or private? In Java, can we declare a top-level class as protected or private? Sep 12, 2023 pm 07:21 PM

No, we cannot declare top-level classes as private or protected. It can be public or default (no modifiers). If there are no modifiers, there should be default access. Syntax //Atoplevelclass publicclassTopLevelClassTest{ //Classbody} If a top-level class is declared as private, the compiler will report an error, prompting "The modifier private is not allowed here." This means that top-level classes cannot be private, the same applies to protected access

Practical application scenarios and usage skills of the static keyword in C language Practical application scenarios and usage skills of the static keyword in C language Feb 21, 2024 pm 07:21 PM

Practical application scenarios and usage skills of the static keyword in C language 1. Overview static is a keyword in C language, used to modify variables and functions. Its function is to change its life cycle and visibility during program running, making variables and functions static. This article will introduce the actual application scenarios and usage techniques of the static keyword, and illustrate it through specific code examples. 2. Static variables extend the life cycle of variables. Using the static keyword to modify local variables can extend their life cycle.

How to use POST request method in jQuery How to use POST request method in jQuery Feb 28, 2024 pm 09:03 PM

How to use the POST request method in jQuery In web development, data interaction between the front-end page and the back-end server is often involved. Among them, POST request is a commonly used method. Through POST request, you can submit data to the backend server and obtain the corresponding return result. jQuery is a popular JavaScript library that provides a convenient way to make AJAX requests. This article will introduce how to use the POST method in jQuery for data transmission and provide specific instructions.

See all articles