Jump to content

PHP OOP: Pipe with 2 different classes


Juergen
 Share

Recommended Posts

Hello @all,

I know how to use chaining with pipe within one class, but I want to know if it is possible to use chaining of methods of 2 different classes. Here is a simple code example:

class A()
{
   protected $text = '';
    
   public function setText(string $value = null)
   {
      $this->text = trim($value);
      return $this;
   }
   
}

class B()
{

    public function addText()
    {
       //instantiate class A
       return new class A();
       return $this;
       ??????????
    }
}

Use it:

$test = new B();
$test->addText->setText();

addText is method of class A
setText is method of class B

As you can see there are 2 different classes (A and B).

A has the method setText() and B has the method addText().

My goal is to create a new instance of class B, then I want to use the method addText(), which is a method of class B and this method should instantiate an object of class A. After that I want to go on with methods of class A in the chaining. So it will be a mix of class A and B.

Is this possible? I have tried to find useful information via Google but without success. Can someone give me a hint, how this can be implemented.

Thanks for your help!

Link to comment
Share on other sites

Hi @Juergen, what about the following:

$y = new Y();
echo $y->addText()->setText('  A B C D  ');


class X {

	protected $text = '';

    public function setText(string $value = null) {
		$this->text = trim($value);
		return $this->text;
    }
}

class Y {

	protected $x;

	public function __construct() {
		$this->x = new X();
	}
	public function addText() {
		return $this->x;
	}
}

I have tested it and it is working, however I did not thought about it too much, it may be possible to do something smarter. Wish you a nice week end.

  • Thanks 1
Link to comment
Share on other sites

Chaining (piping usually means something different) in OOP doesn't mean returning $this. It means return the object, which you want to execute the next method call on. Where you get the object to return from is up to you. But I'm really wondering what the use case behind this is. Generally I'd tend to avoid classes knowing of each other and rather opting for composing their functionality with code outside of them.

  • Like 1
  • Thanks 1
Link to comment
Share on other sites

Hello @LostKobrakai

I have written a class for creating tables.  The first class is the table class itself. The second class is a class for creating table-cells (the content of the table of course). Both class extends from the same parent class (a wrapper class). So both use the same methods from the wrapper class (in this case methods for setting attributes like class, id,...) My aim was to chain methods from the table-cell class inside the table class. My OOP-code of creating a table looks like this in this case:

$table = ( new \UikitClass\Table(3))->setCaption('Beschreibung');
  //thead
  $table->addHeaderCell()->setModifier('expand')->setText('Spalte 1')->setClass('thclass');
  $table->addHeaderCell()->setText('Spalte 2')->setClass('custom');
  $table->addHeaderCell()->setText('Spalte 3')->setClass('custom');
  //tbody
  $table->addCell()->setText('Text 1');
  $table->addCell()->setText('Text 1')->setClass('custom');
  $table->addCell()->setText('Text 1')->setClass('custom');
  $table->addCell()->setText('Text 1')->setClass('custom');
  $table->addCell()->setText('')->setClass('custom');
  //tfoot
  $table->addFooterCell()->setText('Footer')->setClass('uk-text-center')->setAttribute(['colspan' => '3']);
  echo $table->render();

So every chaining starts from $table for easier writing. Therefore I wanted to include the class of the table-cells inside the table class. In this case addHeaderCell(), addCell() and addFooterCell holding an object of the table-cell class and all methods after this affect the table-cell object.

So I start with $table (table-class object), switch to the table-cell object (fe addHeaderCell() method), use methods to the table-cell object and add this to the table class.

Maybe a little bit difficult to explain, but the idea behind was to make writing easier for the developer.

  • Like 1
Link to comment
Share on other sites

 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...