English 中文(简体)
PHP 建筑商和固定功能
原标题:PHP constructors and static functions

我对建筑商如何使用购买力平价感到困惑。

我有一等建筑商,当我看一看一新物体时,就叫他。

$foo = new Foo($args);

。 Foo,实施适当的初始化法。

然而,当我使用这一类别来称为静态功能时,该建筑商被再次传唤。

$bar = Foo::some_function(); //runs the constructor from Foo

这使施工者得以执行,实施我只打算在我创建新的<代码>时使用的物体初始化法。 Foo 反对。

一位我没有说明施工者如何工作? 或者,如果我利用这一类别来进行静态功能电话,则有办法防止<代码>__() 执行?

我是否应当使用“因素”功能,而不是使用目标初步化? 如果是的话,那么什么是建筑商的点?

::EDIT:: I have a form where users can upload photos to an album (create_photo.php) and an area where they can view the album (view_photos.php). Upon form submit:

$photo = new Photo($_FILES[ photo ], $_POST[ arg1 ], ect..);

照片构造制造并节省照片。 然而,在我呼吁的时候,我还是想到:

$photo = Photo::find_by_id($_POST[ id ]) //user-defined function to query database

这使照片的构造变得!!

问题回答

Assumption PHP 5.x

不同目标、不同途径

  1. create a new instance of a class (object)

    class myClassA
    {
       public $lv;
    
       public function __construct($par)
       {
           echo "Inside the constructor
    ";
           $this->lv = $par;
       }
    }
    
    $a = new myClassA(11);
    $b = new myClassA(63);
    

    这是因为我们创建了一个新的目标,即:

    (a) 对新物体的处置;

    $a->lv == 11 
    
    $b->lv == 63
    
  2. use a function of a class

    class myClassB
    {
        public static $sv;
    
        public static function psf($par)
        {
            self::$sv = $par;
        }
    }
    
    myClassB::psf("Hello!");
    $rf = &myClassB::$sv;
    myClassB::psf("Hi.");
    

    现在/code>

    function or variabiles must defined static to be accessed by ::, no object is created calling "psf", the "class variable" sv has only 1 instance inside the class.

  3. a. 使用由工厂创建的单一吨(VClassA以上)

    class myClassC
    {
    
        private static $singleton;
    
        public static function getInstance($par){
    
            if(is_null(self::$singleton)){
    
                self::$singleton = new myClassA($par);
    
            }
    
            return self::$singleton;
    
        }
    
    }
    
    $g = myClassC::getInstance("gino");
    echo "got G
    ";
    
    $p = myClassC::getInstance("pino");
    echo "got P
    ";
    

利用工厂(getInstance),我们第一次构造一个新物体,$ par/em> 。

第二次使用工厂singleton已经具有我们回来的价值。 没有制造新的物体(无 施工<>/em>,称其为“减去记忆和安放”;

页: 1 我 班子,不忘:

myClassC::$singleton->lv == "gino"

Pay attention to singletons:

What is so bad about singletons?

http://www.you Programme.com/watch?v=-FRm3VPhseI

我回答说,我不想促进/帮助单一州。 简言之,我说了这句话:

“静态”+“......建筑”=“singleton”!

这里是我的<>工作:

我在静态类别中采用<代码> 施工()。 通知不同于我在正常班次中使用的---()

每一班都有自己的档案,因此,I lazy装载量在首批使用类别时存档。 这使我第一次使用班级。

spl_autoload_register(function($class) {

    include_once  ./  . $class .  .php ;

    if (method_exists($class,  construct )) {
        $class::construct();
    }
});

我将类别特性定义为固定方法中的阵列,并通过这种方法加以称呼。 我不敢肯定,它是否能够找到最佳解决办法,而是发挥巨大作用。

Example:

    class Foo
    {
      private static construct_method()
      {
        return [
           one  => 1,
           two  => 2
        ];
      }

      public static any_method()
      {
        return self::construct_method()[ one ] + self::construct_method()[ two ];
      }

    }

    echo Foo::any_method(); // 3




相关问题
UserControl constructor with parameters in C#

Call me crazy, but I m the type of guy that likes constructors with parameters (if needed), as opposed to a constructor with no parameters followed by setting properties. My thought process: if the ...

Strange "type class::method() : stuff " syntax C++

While reading some stuff on the pImpl idiom I found something like this: MyClass::MyClass() : pimpl_( new MyClassImp() ) First: What does it mean? Second: What is the syntax? Sorry for being such ...

Anonymous class question

I ve a little doubt over this line: An anonymous class cannot define a constructor then, why we can also define an Anonymous class with the following syntax: new class-name ( [ argument-list ] ) {...

Why copy constructor is not called in this case?

Here is the little code snippet: class A { public: A(int value) : value_(value) { cout <<"Regular constructor" <<endl; } A(const A& other) : value_(other....

Invoking an instance method without invoking constructor

Let s say I have the following class which I am not allowed to change: public class C { public C() { CreateSideEffects(); } public void M() { DoSomethingUseful(); } } and I have to call M ...

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 ...