jakebur01 Posted May 1, 2007 Share Posted May 1, 2007 I have sort of hit a little wall in learning php. I have been going throught a great book called php and mysql web development. I have gotten to the part about creating classes, attributes, and operations. I was really confused about constructors and destructors. Could someone please break down for me the structure of a class? `Jake Quote Link to comment https://forums.phpfreaks.com/topic/49392-solved-classes/ Share on other sites More sharing options...
Caesar Posted May 1, 2007 Share Posted May 1, 2007 Well, I suppose how you use a class depends on the logic or approach of your code. Simplification..... <?php class welcome { function greet() { $msg = new welcome; $name = 'Joe'; $greeting = $msg->hello($name); } function hello($name) { echo"Hello $name. How are you?"; } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/49392-solved-classes/#findComment-242056 Share on other sites More sharing options...
greatwhitepine Posted May 1, 2007 Share Posted May 1, 2007 This should help... <?php class Integer { private $int_i; function __construct( $int_i ) { self::init( $int_i ); // self ensures that if another class is // inherited from this one that the right // init function gets executed } function __destruct( ) { // I haven't ever had any real use for a destructor however you might find one } function reset( ) { $int_i = 0; } function init( $int_i ) { self::reset( ); $this->set( $int_i ); } public function set( $int_i ) { $this->int_i = $int_i; } public function get( ) { return $this->int_i; } public function add( $int_i ) { $this->int_i += $int_i; } } // inheritance -> a class can extend another and it has direct access to all // of the inherited class' public and protected variables (properties) and // functions (methods) class SuperInteger extends Integer { private $str_name; function __construct( $int_i, $str_name ) { parent::init( $int_i ); self::init( $str_name ); } function __destruct( ) { } function reset( ) { $this->str_name = ""; } function init( $str_name ) { self::reset( ); $this->setName( $str_name ); } function setName( $str_name ) { $this->str_name = $str_name; } function getName( ) { return $this->str_name; } } // Regular class $obj_int = new Integer( 123 ); // calls the constructor $obj_int->init( 123 ); // error -> function is private -> can only be called from within the class $obj_int->add( 123 ); print "\n" . $obj_int->get( ) . "\n\n"; // will output 246 unset( $obj_int ); // calls the destructor // Class Inheritance -> notice that the get( ) function can be called as if it was actually // defined within the SuperInteger class $obj_superInt = new SuperInteger( 123, 'a super integer'); // calls the constructor $obj_superInt->add( 123 ); print $obj_superInt->getName( ) . ' ' . $obj_superInt->get( ) . "\n"; // will output a super integer 246 unset( $obj_superInt ); // calls the destructor ?> Quote Link to comment https://forums.phpfreaks.com/topic/49392-solved-classes/#findComment-242057 Share on other sites More sharing options...
greatwhitepine Posted May 1, 2007 Share Posted May 1, 2007 Methods are public by default. Can be called anywhere. The reset() and init() methods should have been declared as protected (i.e. protected function init( ) { ... }). This ensures they can only be called from classes that inherit them. We don't ever want the user to call them directly. Private methods can only be called from with the class that defines them. Quote Link to comment https://forums.phpfreaks.com/topic/49392-solved-classes/#findComment-242059 Share on other sites More sharing options...
jakebur01 Posted May 1, 2007 Author Share Posted May 1, 2007 This helps me see what's going on a little more clearly. I guess I just need to put some of this to use to really see what's going on. What about _get() and _set()? Quote Link to comment https://forums.phpfreaks.com/topic/49392-solved-classes/#findComment-242063 Share on other sites More sharing options...
greatwhitepine Posted May 1, 2007 Share Posted May 1, 2007 I've never used them but there are a couple of examples at http://www.php.net/manual/en/language.oop5.magic.php. I find __toString very handy though. Quote Link to comment https://forums.phpfreaks.com/topic/49392-solved-classes/#findComment-242089 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.