Jump to content

[SOLVED] classes


jakebur01

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/49392-solved-classes/
Share on other sites

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?";
   }

}

?>

Link to comment
https://forums.phpfreaks.com/topic/49392-solved-classes/#findComment-242056
Share on other sites

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

 

 

?>

Link to comment
https://forums.phpfreaks.com/topic/49392-solved-classes/#findComment-242057
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/49392-solved-classes/#findComment-242059
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.