2

so I am new in the world of object oriented programming and I am currently facing this problem (everything is described in the code):

<?php
    class MyClass {
        // Nothing important here
    }

    class MyAnotherClass {
        protected $className;

        public function __construct($className){
            $this->className = $className;
        }
        public function problematicFunction({$this->className} $object){
            // So, here I obligatorily want an $object of
            // dynamic type/class "$this->className"
            // but it don't works like this...
        }
    }

    $object = new MyClass;
    $another_object = new MyAnotherClass('MyClass');

    $another_object->problematicFunction($object);
?>

Can anyone help me ?

Thanks, Maxime (from France : sorry for my english)

2
  • you can't use {$this->className}, check $object with instanceof in problematicFunction Commented Feb 25, 2013 at 10:55
  • What you want to achieve with this? Autoloader? Commented Feb 25, 2013 at 10:57

3 Answers 3

3

What you need is

public function problematicFunction($object) {
    if ($object instanceof $this->className) {
        // Do your stuff
    } else {
        throw new InvalidArgumentException("YOur error Message");
    }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Try like this

class MyClass {
    // Nothing important here
    public function test(){
        echo 'Test MyClass';
    }
}

class MyAnotherClass {
    protected $className;

    public function __construct($className){
        $this->className = $className;
    }
    public function problematicFunction($object){
        if($object instanceof $this->className)
        {
            $object->test();
        }
    }
}

$object = new MyClass;
$another_object = new MyAnotherClass('MyClass');

$another_object->problematicFunction($object);

Comments

0

That's called type hinting and what you want to do is just not supported.

If all those dynamic class names have something in common (e.g., they're different implementations for certain feature) you probably want to define a base (maybe abstract) class or an interface and use that common ancestor as type hint:

<?php

interface iDatabase{
    public function __contruct($url, $username, $password);
    public function execute($sql, $params);
    public function close();
}

class MyClass implements iDatabase{
    public function __contruct($url, $username, $password){
    }

    public function execute($sql, $params){
    }

    public function close(){
    }
}

class MyAnotherClass {
    protected $className;

    public function __construct($className){
        $this->className = $className;
    }
    public function problematicFunction(iDatabase $object){
    }
}

Otherwise, just move the check to within problematicFunction() body, as other answers explain.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.