0

I'm new in PHP OOP and curious about method. How to make method inside method (I dont know what its name)? For example, I can access like this

<?php
$myClass = new CarClass;
$myClass->createNew->bodySection->setColor("red");

Just like Codeigniter for calling a Models or Library using this.

<?php
$this->myLibrary->getData()

It's different from method chaining where between method call there is no parameter, its like javascript.

Can I achieve that? Or any alternative?

Thank you

Raissa Ditya Putri
  • 89
  • 1
  • 1
  • 10
  • Put an object in the `createNew` property. The object that it points to should have a `bodySection` property that contains another object. And that last object has a `setColor()` method. – Barmar Jan 23 '20 at 02:12
  • please check sample code – sajjad Jan 23 '20 at 02:46
  • This question appears to be a duplicate of [PHP method chaining?](https://stackoverflow.com/questions/3724112/php-method-chaining) – Sherif Jan 23 '20 at 02:47
  • @Sherif, no, its different, I mean ```$myClass->createNew->bodySection->setColor("red")```, not ```$myClass->createNew()->bodySection()->setColor("red")``` – Raissa Ditya Putri Jan 23 '20 at 02:55
  • Just "categorize" method inside a class – Raissa Ditya Putri Jan 23 '20 at 02:59
  • `$myClass->createNew->bodySection` would just be a property that contains another object that contains another object that contains a method. This has nothing to do with methods at all or nesting methods or even chaining them. This is just basic instance object dereferencing. Nothing special here. i.e. the question itself makes no sense. – Sherif Jan 23 '20 at 03:02
  • Sorry, can you give me sample code @Sherif? I don't get it. – Raissa Ditya Putri Jan 23 '20 at 03:04
  • I just did. This question lacks focus I'm afraid. Perhaps consider reading [How do I ask a good question](https://stackoverflow.com/help/how-to-ask) before proceeding any further? – Sherif Jan 23 '20 at 03:12

1 Answers1

1

Given the code,

$myClass = new myCar;
$myClass->createNew->bodySection->setColor("red");

we can make the following statements:

  1. myCar has a property named “createNew”.
  2. createNew holds some unknown object
  3. The unknown object has a property called bodySection
  4. The property named bodySection contains an unknown object that has a method named setColor()

Clear as mud?

There are several ways this could be illustrated; here’s one:

class myCar {

    public createNew;

    public function __construct() {
        $this->createNew = new Foo;
    }
}

class Foo {

    public bodySection;

    public function __construct() {
        $this->bodySection = new Bar;
    }
}

class Bar {

    public function setColor($color) {
        echo "Color is $color";
    }
}

$myClass = new MyClass;
$myClass->createNow->bodySection->setColor('red');

// output: Color is red

The first problem here is that “createNow” doesn’t make sense as a property; it’s an action, not something that a myCar would own or do.

Likewise, a bodySection would probably have a color as a property, to be set with its own setter method, not some external object.

Bottom line, making long chains of pointers is not something to seek after; rather, they’re probably better kept as short as possible. Otherwise your object probably knows too much about to many things.

Tim Morton
  • 2,614
  • 1
  • 15
  • 23