...

View Full Version : Nested Classes?



Wolf95
01-20-2012, 03:36 AM
Hello, I was wondering if nested classes would work so they can be called like this:

$class->subclass->subclass'smethod();
and if subclass would be able to access class's variables like child functions?
-Thanks.

Dormilich
01-20-2012, 07:53 AM
1) that's not a nested class
2) access like that won't work

in the expression $class->subclass->subclass_method(); it's just that the class's property subclass is an object itself. whether this object was passed down to class or was created inside class does not matter.

ex.

// bear in mind that this code is only for demonstration
// since the class design is pretty bad
class base
{
public $subclass;
}
class child
{
public function someMethod()
{
// do something ...
}
}

// create a new base class
$class = new base;
// create a new child class
$child = new child;
// attach child to base
$class->subclass = $child;
// call subclass method
$class->subclass->someMethod();
design-wise it is a bad idea to make the subclass property public because public properties can be changed without control. let's assume the following:

$class = new base;
$class->subclass = new child;
// ...
$class->subclass = "awesome comment";
$class->subclass->someMethod();
this code will fail with a fatal error since subclass is now a string and not an object.

however, instead of a public property you can use setters and getters

class base
{
private $subclass;

// define a setter. this code verifies via type hinting that $subclass
// is actually a child instance
public function setSubclass(child $subclass)
{
$this->subclass = $subclass;
}

// and while we're at it, we define a getter as well
public function getSubclass()
{
// error out if subclass has not been defined yet
if (!($this->subclass instanceof child))
{
throw new Exception("Subclass does not exist");
}
return $this->subclass;
}
}

// define base class
$class = new base;
// define subclass
$child = new child;
// pass child to base
$base->setSubclass($child);
// call child class
// will throw an exception if subclass has not been set before
$base->getSubclass()->someMethod();

you could get even more generic. if you only need to call the someMethod() method out of class, then defining an interface loosens the coupling between base & child:

interface DoSomething
{
public function someMethod();
}
class child implements DoSomething
{
public function someMethod()
{
// do something ...
}
}
class enkel implements DoSomething
{
public function someMethod()
{
// do something ...
}
public function doSomething()
{
// do something else ...
}
}
// now it doesn't matter if we're passing the child class or the enkel class
// the code will work both ways

class base
{
private $subclass;

// define a setter. this code verifies via type hinting that $subclass
// HAS A someMethod() FUNCTION
public function setSubclass(DoSomething $subclass)
{
$this->subclass = $subclass;
}

// and while we're at it, we define a getter as well
public function getSubclass()
{
// error out if subclass has not been defined yet
// since we used an interface for type checking, wee need to use the same interface here
if (!($this->subclass instanceof DoSomething))
{
throw new Exception("Subclass does not exist");
}
return $this->subclass;
}
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum