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 8056741
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T08:48:46+00:00 2026-06-05T08:48:46+00:00

I’ve been reading through Effective Java by Joshua Bloch. I also develop in PHP

  • 0

I’ve been reading through Effective Java by Joshua Bloch. I also develop in PHP and I wanted to implement the builder pattern outlined in item 2, but PHP doesn’t have inner classes. Is there any way to achieve this pattern in PHP, keeping the constructor for the product private?

  • 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-06-05T08:48:48+00:00Added an answer on June 5, 2026 at 8:48 am

    Since PHP does not support inner classes, there must be a public method on the product class that creates an instance of it. Consider the following PHP classes:

    <?php
    class NutritionalFactsBuilder {
        private $sodium;
        private $fat;
        private $carbo;
    
        /**
         * It is preferred to call NutritionalFacts::createBuilder
         * to calling this constructor directly.
         */
        function __construct($s) {
            $this->sodium = $s;
        }
    
        function fat($f) {
            $this->fat = $f;
            return $this;
        }
    
        function carbo($c) {
            $this->carbo = $c;
            return $this;
        }
    
        function getSodium() {
            return $this->sodium;
        }
    
        function getFat() {
            return $this->fat;
        }
    
        function getCarbo() {
            return $this->carbo;
        }
    
        function build() {
            return new NutritionalFacts($this);
        }
    }
    
    class NutritionalFacts {
        private $sodium;
        private $fat;
        private $carbo;
    
        static function createBuilder($s) {
            return new NutritionalFactsBuilder($s);
        }
    
        /**
         * It is preferred to call NutritionalFacts::createBuilder
         * to calling this constructor directly.
         */
        function __construct(NutritionalFactsBuilder $b) {
            $this->sodium = $b->getSodium();
            $this->fat = $b->getFat();
            $this->carbo = $b->getCarbo();
        }
    }
    
    echo '<pre>';
    var_dump(NutritionalFacts::createBuilder(10)->fat(23)->carbo(1)->build());
    echo '</pre>';
    ?>
    

    Note that in the above example the constructor of NutritionalFacts is public. Due to the constraints of the language, however, having a public constructor is not at all bad. Since one must call the constructor with a NutritionalFactsBuilder, there are only a limited number of ways to instantiate NutritionalFacts. Let’s compare them:

    // NutritionalFacts Instantiation #0
    $nfb = new NutritionalFactsBuilder(10);
    $nfb = $nfb->fat(23)->carbo(1);
    $nf0 = new NutritionalFacts($nfb);
    
    // NutritionalFacts Instantiation #1
    $nfb = new NutritionalFactsBuilder(10);
    $nf1 = $nfb->fat(23)->carbo(1)->build();
    
    // NutritionalFacts Instantiation #2
    $nf2 = NutritionalFacts::createBuilder(10)->fat(23)->carbo(1)->build();
    
    // NutritionalFacts Instantiation #3
    // $nf3 = (new NutritionalFactsBuilder(10))->fat(23)->carbo(1)->build();
    

    To leverage function chaining to its fullest extent, “NutritionalFacts Instantiation #2″ is the preferred usage.

    “NutritionalFacts Instantiation #3″ shows another nuance of PHP syntax; one cannot chain a method on a newly instantiated object. Update: In PHP 5.4.0, there is now support for the syntax in “NutritionalFacts Instantiation #3.” I haven’t tested it yet though.


    Making the Constructor Private

    You could make the constructor private, but I wouldn’t recommend it. If the constructor were made private, a public, static factory method would be necessary, as in the following code snippet. Looking at the below code, we might as well make the constructor public instead of introducing indirection just to make the constructor private.

    class NutritionalFacts {
        private $sodium;
        private $fat;
        private $carbo;
    
        static function createBuilder($s) {
            return new NutritionalFactsBuilder($s);
        }
    
        static function createNutritionalFacts($builder) {
            return new NutritionalFacts($builder);
        }
    
        private function __construct($b) {
            $this->sodium = $b->getSodium();
            $this->fat = $b->getFat();
            $this->carbo = $b->getCarbo();
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have thousands of HTML files to process using Groovy/Java and I need to

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.