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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T03:06:54+00:00 2026-05-16T03:06:54+00:00

i was wanting to wrap an object in another – favoring composition over inheritance.

  • 0

i was wanting to wrap an object in another – favoring composition over inheritance. but i am not sure i am doing it right tho there are no errors.

i created a class Wrapped thats is wrapped by Wrapper. i made it such that when a method/property is called on $wrapper, if it exists in the class, Wrapper, it will be returned else, it will delegate to the $wrapped object. i wonder apart from the fact i didnt check if the method/property exists, what have i done wrong? can some1 explain __callStatic() too?

class Wrapped {
    protected $wrappedProp1 = 'Wrapped: Property 1';
    protected $wrappedProp2 = 'Wrapped: Property 2';

    function method1($arg1, $arg2) {
        echo "Called Wrapped::method1() with the following parameters: $arg1, $arg2";
    }

    static function sMethod2() {
        echo 'Called a static method in wrapped';
    }

    function __get($name) {
        return $this->$name;
    }
    function __set($name, $val) {
        $this->$name = $val;
    }
}
class Wrapper {
    protected $wrapperProp1 = 'Wrapper: Property 1';
    protected $wrapped;

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

    function wrapperMethod() {
        echo 'In wrapper method';
    }

    function __get($name) {
        if (property_exists($this, $name)) {
            return $this->$name;
        }
        return $this->wrapped->$name;
    }
    function __set($name, $val) {
        if (property_exists($this, $name)) {
            $this->$name = $val;
        }
        $this->wrapped->$name = $val;
    }
    function __call($name, $args = array()) {
        call_user_func_array(array($this->wrapped, $name), $args);
    }
    static function __callStatic($name, $args = array()) {
        call_user_func_array(array('Wrapped', $name), $args);
    }
}

$wrapper = new Wrapper(new Wrapped);

// testing normal methods
$wrapper->wrapperMethod();
echo $wrapper->wrapperProp1;
$wrapper->wrapperProp1 = 'New Wrapper Prop 1';
echo $wrapper->wrapperProp1;

// testing delegates
$wrapper->method1('hello', 'world'); //delegated to Wrapped::method1()
$wrapper->sMethod2(); // delegated to static Wrapped::sMethod2() ... what is callStatic for then
echo $wrapper->wrappedProp2;
Wrapper::sMethod2();
  • 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-16T03:06:55+00:00Added an answer on May 16, 2026 at 3:06 am

    As it seems – it’s all ok.

    About __callStatic() – it allows you to workaround undefined static functions in class.
    Example:

    <?php
    class Foo {
        static function __callStatic($name, $args = array()) {
            echo "Called static function $name with arguments ". print_r($args, true);
        }
    }
    
    Foo::Bar('test');
    // will output "Called static function Bar with arguments Array ( 0 => test );"
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm not sure if this is possible in the manner I am envisioning or
I'm a newbie to PowerShell. What's wrong with my script below? It's not wanting
I'm wanting to use jQuery to wrap a mailto: anchor around an email address,
I want to know, is there the possibility to ask Unity do not wrap
I'm trying to use setRetainInstance() but it seems not working for me! =(. I
Wanting to give byobu a looksee as a screen 'update' of sorts. I start
Wanting to integrate aspects of SharePoint into an existing website - rather than integrating
I'm wanting a more secure way to store high scores and power ups like
I'm wanting to extract a zip file loaded with images into memory in some
I'm wanting to figure out why the th checkbox never gets aligned with the

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.