April 9, 2012

Force variable pass as object

Question by Gabriel Santos

I need to define the type of passed variable as an generic object, how?

Tried:

public function set($service = null, (object) $instance) {
    [..]
}

Can stdClass help? How?

Thanks!

Answer by zerkms

Nope, in php all classes aren’t derive from the common ancestor.

So doubtfully you can use current php’s implementation to state “object of any class”

Answer by Starx

No, the object has to be some class. You can give the any class name as object’s type

public function set($service = null, ClassName $instance) {
    //now the instance HAS to be the object of the class
}

Or a basic trick would be to create a basic class yourself

Class GenericObject {}
$myobj = new GenericObject();
$myobj -> myCustomVar = 'my custom var';

//Now send it
public function set($service = null, GenericObject $instance) {
   [...]
}

Author: Nabin Nepal (Starx)

Hello, I am Nabin Nepal and you can call me Starx. This is my blog where write about my life and my involvements. I am a Software Developer, A Cyclist and a Realist. I hope you will find my blog interesting. Follow me on Google+

...

Please fill the form - I will response as fast as I can!