在php中扩展一个类到另一个类


Extending one class inside other class in php

我有一个类a,它扩展了类B,如下所示

Class A extends B
{

    function SendMAil(){
    /* Here Send Mail Code is wriiten */ 
    }
}

和我有另一个类C,我已经实现了多线程的概念,为此我需要从线程

扩展C
Class C extends Thread {

  function SendMail2(){
  /* Code For SendMail2() */

  }
}

现在我需要扩展线程类也在类A,所以我可以在类A中使用多线程的功能,我怎么能做到这一点

PS:类Thread是一个内置的类,当我们为php安装pthread库时,我们不能对类Thread做任何修改。

我卡在那里了!!我怎么能这么做??

提前感谢!!

你应该使用trait。Trait很像类,但是它们更可能被用来捆绑你非常需要的东西,并且它们之间有关系。

例如:

trait ThreadTrait {
    public function doSomeMultithreading() {
        // Put your multithreading code in here
    }
}
Class A extends B {
    use ThreadTrait;
    public function aFunctionWhereYouCanUseMultithreadingNow() {
        echo 'Multithreading possible in every class now!:)';
    }
}

现在你可以在你想要的每个类中访问多线程所需的所有函数。

可以先用Thread类扩展B类,然后用B类扩展A类。您将在B类中获得Thread类的所有公共和受保护成员。这样的:

<?php 
class Thread {
    public function threadfunc() {
        echo "This is thread class";
    }
}
class B extends Thread {
    public function bfunc()
    {
        $this->threadfunc();
    }
}
Class A extends B {
    function SendMAil(){
        echo "sendmail";
    }
}
Class C extends Thread {
    function SendMail2(){
        echo "send mail 2";
    }
}
$obj = new A();
$obj->bfunc();

性状在这里没有用,因为它们只能由其它性状组成;一个性状不能extend一个类,但只能use其他性状。

你所描述的继承是相当奇怪的,不管pthreads。

关于pthreads, Threaded对象与普通PHP对象的语义不同,普通PHP对象没有很好地准备线程,这是非常好的理由,当你打算在多线程代码中使用时,你需要声明类是从pthreads派生的。

所以,一个例子;因为A, B和C有点抽象;)

如果我们想象你的后端应用程序负责注册新用户;更新数据库,发送电子邮件,创建小猫头像(显然),以及做一些其他繁重的工作。

我们将做如下假设:

  • 在你的应用程序的某个地方你有一个Pool,因为这是一个很好的实践,
  • 你有一些类设计做后台工作(Threaded)
  • 你有一些正常的PHP类做正常的事情
<标题>梅勒h1> em>线程化的代码
class Mailer extends Threaded {
    public function __construct($email, $subject, $message) {
        $this->email = $email;
        $this->subject = $subject;
        $this->message = $message;
    }
    public function send() {
        if (!mail($this->email, $this->subject, $this->message)) {
            throw new 'RuntimeException(
                "the mailman is a failure ...");
        }
    }
    public function run() {
        /* when I am submitted to a 
            Pool or Worker, I will send the email */
        $this->send();
        /* alternatively, Mailer could be a Thread
            but I won't encourage you to be wasteful ;) */
    }
    protected $email;
    protected $subject;
    protected $message;
}
<标题>任意代码

非线程化的应用程序代码

class Something {
    public function thing() {
    }
}
<标题> 应用程序代码

使用任意线程代码的代码

class Register {
    public function __construct(Pool $pool, Mailer $mail, Something $some) {
        $this->pool = $pool;
        $this->mail = $mail;
        $this->some = $some;
    }
    public function doRegistration() {
        /* ... do registration things ... */
        $this->some->thing();
        /* send the email in the background */
        $this->pool
            ->submit($this->mail);
        /* do the next thing */
        $this->some->thing();
    }
    protected $pool;
    protected $mail;
    protected $other;
}
<标题> 例子
/* somewhere else in the application */
$pool     = new Pool(2);
$some     = new Something();
/* here */
$mail     = new Mailer("krakjoe@php.net", "oi", "look here");
$register = new Register($pool, $mail, $some);
$register->doRegistration();
/* somewhere else suitable in the application */
$pool->shutdown();
http://pastebin.com/PvaZr2pH

注意:没有必要在后台线程中发送电子邮件,这只是示例代码

进一步阅读:

  • https://gist.github.com/krakjoe/6437782
  • https://gist.github.com/krakjoe/9384409
  • http://php.net/pthreads