`
berrywu
  • 浏览: 127925 次
  • 性别: Icon_minigender_1
  • 来自: 福建
社区版块
存档分类
最新评论

Using $this when not in object context

阅读更多

今天在写脚本的时候,遇到了一个很郁闷的问题,脚本比较繁琐,以下用一个简单的例子表示

<?php
   class fun{
       var a = '1';
       function __contruct(){
           echo 'this is contruct';
       }
       function test(){
   
          echo $this->a;
      }
   }

?>

 

<?php
    require_once('./class.php');
    echo fun::test();
?>

 

该脚本的运行环境在PHP5下,报错Fatal error: Using $this when not in object context

又试了一下,在PHP4环境的可以运行

我想PHP4到PHP5升级后,提高了代码的安全性。

上述的代码是通过命名空间的方式调用的,这个函数虽然在类里面,但是被调用后只是普通的函数了,所以解释不到$this

因此,若要在PHP5下调用类fun下的test方法,可以先继承这个类,然后new一个对象来调用父类的方法

<?php
    require_once('./class.php');
    class exam extends fun{
        function __contruct(){
             echo 'this is child contruct';
        }
    }
    $obj = new exam;
    echo $obj->test;

?>

 注意,若不想让父类fun执行它的构造函数,继承类必须写上构造函数。

 

当然,若在PHP4环境下是不会出现以上问题的

有人会问了,我直接new fun这个类来调用不就好了吗,干嘛那么麻烦还搞继承。其实以上只是个例子而已,我今天写的脚本是调用某个项目里面的一个类,这个类当然不是专门给你用的,整个类包含太多你不需要的东西,而你只要用它类里面的一个方法而已,还有一个原因是我不想让父类自动执行构造函数,所以这么写还是有必要的。

 

分享到:
评论

相关推荐

    浅谈PHP中静态方法和非静态方法的相互调用

    因此,尝试在静态方法中使用`$this`会导致“Using $this when not in object context”的致命错误。 为了在静态方法中访问非静态方法或属性,必须首先创建一个类的实例,并通过这个实例来调用所需的非静态方法。...

    详谈php静态方法及普通方法的区别

    如果尝试在静态方法中使用$this,将会得到一个致命错误:"Using $this when not in object context"。 在PHP的错误报告级别设置得严格的情况下,尝试用类名静态调用普通方法也会得到警告或错误,因为这种方式并不...

    Sortable前端框架

    unwanted touchmove events even when your finger is not moving, resulting in the sort not triggering. This option sets the minimum pointer movement that must occur before the delayed sorting is ...

    Using DIB Sections in VB

    This was because the code was not always clearing up the SAFEARRAY pointer after it had finished using it. Under Win9x, this did not cause a problem but NT is stricter. All the samples and the code ...

    Object-Oriented Software Construction 2nd

    1.4 KEY CONCEPTS INTRODUCED IN THIS CHAPTER 19 1.5 BIBLIOGRAPHICAL NOTES 19 Chapter 2: Criteria of object orientation 21 2.1 ON THE CRITERIA 21 2.2 METHOD AND LANGUAGE 22 2.3 IMPLEMENTATION AND ...

    servlet2.4doc

    Returns the object bound with the specified name in this session, or null if no object is bound under the name. getAttributeNames() - Method in interface javax.servlet.ServletContext Returns an ...

    Thinking in LINQ

    This chapter sets the context for the rest of the book. • Chapter 2: Series Generation This chapter has recipes for generating several series using LINQ. For example, it shows how to generate ...

    FlexGraphics_V_1.79_D4-XE10.2_Downloadly.ir

    - FIX: The TFlexRegularPolygon object clones incorrectly drawed in case when TFlexRegularPolygon have alternative brush (gradient, texture). - ADD: Add TFlexPanel.InvalidateControl virtual method ...

    数位板压力测试

    In this document, the words "tablet" and "digitizer" are used interchange¬ably to mean all absolute point¬ing or digitizing devices that can be made to work with this interface. The definition is ...

    VclZip pro v3.10.1

    Some optimization improvements which should show some improvement in zipping and unzipping speed when using TkpStreams with D4, D5, BCB4, and BCB5. ============ Version 3.03 (VCLZip Pro) - Please ...

    JSP Simple Examples

    In this program we are going to know how the server determines whether the password entered by the user is correct or not. This whole process is controlled on the server side. Multiple forms in jsp ...

    DevExpress VCL v2012 vol 1.6源码、例子、帮助-part2

    Q432087 - cxDateEdit controls are not displayed on a detail page when activating this page for the second and subsequent times in Windows XP B218389 - Focus does not move to the previous/next row when...

    Packt.Python.Journey.from.Novice.to.Expert.2016

    Learn the trickier aspects of Python and put it in a structured context for deeper understanding of the language Who This Book Is For This course is meant for programmers who wants to learn Python ...

    Mastering Lambdas- Java Programming in a Multicore World

    - **Avoid Side Effects**: When using streams, ensure that the operations do not have unintended side effects, as this can lead to unpredictable behavior. - **Consider Performance Implications**: While...

    DevExpress VCL v2012 vol 1.6源码、例子、帮助

    Q432087 - cxDateEdit controls are not displayed on a detail page when activating this page for the second and subsequent times in Windows XP B218389 - Focus does not move to the previous/next row when...

    DevExpress VCL v2012 vol 1.6源码、例子、帮助-Part1

    Q432087 - cxDateEdit controls are not displayed on a detail page when activating this page for the second and subsequent times in Windows XP B218389 - Focus does not move to the previous/next row when...

    DevExpress VCL 2012 vol 1.6源码、例子、帮助-Part2

    Q432087 - cxDateEdit controls are not displayed on a detail page when activating this page for the second and subsequent times in Windows XP B218389 - Focus does not move to the previous/next row ...

    DevExpress VCL 2012 vol 1.6源码、例子、帮助-Part1

    Q432087 - cxDateEdit controls are not displayed on a detail page when activating this page for the second and subsequent times in Windows XP B218389 - Focus does not move to the previous/next row ...

    微软内部资料-SQL性能优化2

    Because device drivers operate at DPC/dispatch level (covered in lesson 2), and page faults are not allowed at this level or above, most device drivers use non-paged pool to assure that they do not ...

    微软内部资料-SQL性能优化3

    In this scenario, since T1 believes it locks the entire table, it might inadvertently make changes to the same row that T2 thought it has locked exclusively. In a multigranular locking environment, ...

Global site tag (gtag.js) - Google Analytics