Page 1 of 1

Class Object

Posted: Mon Jul 24, 2006 11:38 pm
by denisw
I have few questions regarding the Class object in PHP :

1. is there a way to restrict the creation of class object only from particular other class object ? so, the user won't be permitted to explicitly create object by issuing NEW object() from main script.
User can only get the object by this way :
$myobj=$parobj->create_obj();

2. I made some object initialization via constructor function, whereby the constructor will possibly return TRUE or FALSE condition. I wonder how to check the result value returned by constructor function ?(without calling this constructor manually).

thanks.

Posted: Mon Jul 24, 2006 11:45 pm
by wtf
I think what you are looking for is abstract class.

This should be a good start->http://www.devshed.com/c/a/PHP/Abstract ... ith-PHP-5/

Posted: Mon Jul 24, 2006 11:47 pm
by feyd
  1. That would depend on what version of PHP you are talking about. In PHP 5, you can privatize the constructor, but in 4, not possible.
  2. Not possible, that I know of. The class should set an internal property.

Posted: Tue Jul 25, 2006 3:25 am
by Jenk
If using PHP5 you can use an exception as a pseudo-boolean..

Code: Select all

class MyClass
{
    public function __construct ($sky, $colour = 'blue')
    {
        if ($sky != $colour) {
            throw new Exception ('blah');
        }
    }
}

try {
    $foo = new MyClass('grey');
} catch (Exception $e) {
    //do something here
}

?>