Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7508633
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:42:27+00:00 2026-05-29T22:42:27+00:00

The typical way to use : <?php class A { public $var; public function

  • 0

The typical way to use :

<?php

class A {
  public $var;

  public function __construct($var){
    $this->var = $var;
  }

  public function do_print(){
    print $this->var;
  }
}

?>

$obj = new A('Test');
$obj->do_print(); // Test

How can I implement something like:

$obj->var->method();

And why is this useful?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:42:29+00:00Added an answer on May 29, 2026 at 10:42 pm

    By making var an object of another class, you can chain the method calls to one another.

    <?php
        class Foo {
            public $bar;
            public function __construct(Bar $bar) {
                $this->bar= $bar;
            }
        }
    
        class Bar {
            private $name;
            public function __construct($name) {
                $this->name = $name;
            }
            public function printName() {
                echo $this->name;
            }
        }
    
        $bar = new Bar('Bar');
        $bar2 = new Bar('Bar2');
        $foo = new Foo($bar);
    
        $foo->bar->printName(); // Will print 'Bar';
        $bar2->printName(); // Will print 'Bar2'
    

    You can use this for neat things like Dependency Injection

    Also, it may make your code bette readable and understandable because you don’t have to buffer variables before making calls to their methods and can just call one method after the other.

    Look at this example:

    $obj = new MyObject();
    $db = $obj->getDb();
    $con = $db->getCon();
    $stat = $con->getStat();
    

    Which could be written like this using method chaining:

    $obj = new Object();
    $stat = $obj->getDB()->getCon()->getStat();
    

    But, this is also harder to debug because if any of these methods throw an exception, you will just get the line number where the chain is, which can be quite a hazzle.

    So, there are always two sides. It’s just another style of programming.

    Just be sure not to chain too long in a single line, as you will definitely lose overview.

    BAD

    $obj->meth('1', $arg2, array('arg2'))->method2($whaterver, array('text' => $bla_text))->andSoOn();
    

    GOOD

    $obj->meth('1', $arg2, array('arg2'))
        ->method2($whaterver, array('text' => $bla_text))
        ->andSoOn();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Typical jQuery over-use: $('button').click(function() { alert('Button clicked: ' + $(this).attr('id')); }); Which can be
The typical way to define an integer constant to use inside a function is:
Consider this example (typical in OOP books): I have an Animal class, where each
The typical way of implementing IUnknown::QueryInterface() is the following: use a chain of if-else-if
A typical way to use JFileChooser includes checking whether user clicked OK, like in
I would like to use the better styled UIBarButtonType as a typical UIButton. This
Typical way of creating a CSV string (pseudocode): Create a CSV container object (like
The typical way of selecting data is: select * from my_table But what if
I'm learning socket programming (in python) and I was wondering what the best/typical way
During a typical day programming, I implement functions in a way that I would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.