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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T16:54:13+00:00 2026-06-01T16:54:13+00:00

As I am working on a piece of code that needs the decorator pattern,

  • 0

As I am working on a piece of code that needs the decorator pattern, I wanted to make it really simple to use by handling the __call magic method.

As a matter of fact, when I use the decorator pattern (here, to add a singleton, add some methods and forbid some others) some of the methods have no need to be overridden. So using __call is a good way to simplify the code.

My situation raises when some methods need arguments passed by reference.

To give an example, I created a XPDO class that decores PDO. It is not my former case but I can’t show that one.

<?php

class XPDO{
    private static $dbInstance=null;
    private $pdoConnexion;
    static function getInstance(){
        if(self::$dbInstance ==null){
            self::$dbInstance = new XPDO(/*tes params*/);
        }
        return self::$dbInstance;

    }
    private function __clone(){
    }
    private function __construct(){
        $this->pdoConnexion = new PDO('mysql:localhost;dbname=blog','root','');
    }
    /**
    *on possède toutes les méthodes de PDO mais en plus certaines qui nous sont propres ou qui
    *surchargent/limitent celles de PDO si telles qu'elles sont implémentées dans PDO, on ne les aime pas.
    */
    public function __call($method, $args){
        if(is_callable(array($this,$method))){
            return call_user_func_array(array($this,$method),$args);
        }else if(is_callable(array($this->pdoConnexion,$method))){
            return call_user_func_array(array($this->pdoConnexion,$method),$args);
        }
    }

    /**
    *
    *@param string $query the query we want to add the where
    *@param string $param name of the column
    *@return string the identifier that we would use to bind a value
    */
    private function addAndWhere(&$query,$param){
        $uid = rand(1,100000);
        if(strpos($query,'WHERE')){

            $query.= ' AND '.$param.'=:'.$param.$uid;
        }else{
            $query.= ' WHERE '.$param.'=:'.$param.$uid;
        }
        return $param.$uid;
    }
}
$pdo = XPDO::getInstance();
$query = 'SELECT * FROM sometable';
var_dump($pdo->addAndWhere($query,'smth'));
var_dump($query);

this would fail because addAndWhere expects a reference and a copy is given.
This code can be easily fixed by passing addAndWhere to public, and it has sense. Here it is just an example. Now imagine it is PDO that needs a reference and you got my point.

  • 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-01T16:54:14+00:00Added an answer on June 1, 2026 at 4:54 pm

    From php manual in Overloading page

    None of the arguments of these magic methods can be passed by reference.

    there are no clean solutions.

    You can do only

    $pdo->addAndWhere(&$query,'smth');
    

    but this is DEPRECATED since 5.3, with relative warning.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a piece of code that I inherited and am trying to
I'm working on a piece of code for an iPhone application that fetches a
I am currently working a piece of code which needs to send an attachment
I am working on a piece of code that writes data from 4 different
I am working on a piece of arduino code that is using the BlackWidow
Let's say you are working on a piece of legacy code that was written
I am working on a piece of code that I am wanting to spice
I'm working on a piece of code at the moment that allows the user
I'm working a piece of code to turn phone numbers into links for mobile
I am working on a piece of code which uses regular expressions in c.

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.