English 中文(简体)
如何获得制造目前物体的类别?
原标题:How to get the class that created the current object?
  • 时间:2010-08-02 09:53:56
  •  标签:
  • php
  • class
  • oop

我想防止 f( f)由除B以外的任何其他类别人员执行。 我如何检查哪类制造物体A?

<?php
class A
{
    public function foo()
    {
        if (.... B )    // what should be on the dotts?
           echo  I m created by class B, which is fine ;
        else
           echo  Execution of foo() is not allowed ;
    }
}

class B
{
    public function go()
    {
        $a = new A;
        $a->foo();
    }
}

class C
{
    public function go()
    {
        $a = new A;
        $a->foo();
    }
}

$b = new B;
$b->go();  // result: I m created by class B, which is fine

$c = New C;
$c->go();  // result:  Execution of foo() is not allowed 
最佳回答
问题回答

Declare foo, 载于B, 改成“私人”和“任择”形式。 为什么要在<代码>中界定 A 仅可通过查询的方法 B





相关问题
Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

JSON with classes?

Is there a standardized way to store classes in JSON, and then converting them back into classes again from a string? For example, I might have an array of objects of type Questions. I d like to ...

Object-Oriented Perl constructor syntax and named parameters

I m a little confused about what is going on in Perl constructors. I found these two examples perldoc perlbot. package Foo; #In Perl, the constructor is just a subroutine called new. sub new { #I ...

Passing another class amongst instances

I was wondering what is the best practice re. passing (another class) amongst two instances of the same class (lets call this Primary ). So, essentially in the constructor for the first, i can ...

Where can I find object-oriented Perl tutorials? [closed]

A Google search yields a number of results - but which ones are the best? The Perl site appears to contain two - perlboot and perltoot. I m reading these now, but what else is out there? Note: I ve ...

热门标签