Class Object

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
denisw
Forum Newbie
Posts: 14
Joined: Mon Jul 24, 2006 11:21 pm

Class Object

Post 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.
User avatar
wtf
Forum Contributor
Posts: 331
Joined: Thu Nov 03, 2005 5:27 pm

Post 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/
Last edited by wtf on Mon Jul 24, 2006 11:49 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Jenk
DevNet Master
Posts: 3587
Joined: Mon Sep 19, 2005 6:24 am
Location: London

Post 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
}

?>
Post Reply