Jump to content

php classes


Recommended Posts

A class with a method with a name same than it's parent class does execute the method immediatly
That's right, that's called a constructor. You can also have a destructor that gets executed when the instance gets deleted. But that syntax is with PHP 4 style classes. For PHP 5 style (which allow destructors), it looks like this:
<?phpclass MyDestructableClass {   function __construct() {       print "In constructor\n";       $this->name = "MyDestructableClass";   }   function __destruct() {       print "Destroying " . $this->name . "\n";   }}$obj = new MyDestructableClass();?>

To call a function you dont put new functionname() in front of it, if you do please correct me.
That's correct, but he was creating instances of his class, not creating or calling functions. However, he did mess up because he didn't name his constructor WWWLanguage, he only named it Language, so it's not a constructor and won't get executed on instantiation. You would have to explicitly call $var->Language() to use it.
BTW, what's the difference between public and private variables?
public variables can be directly accessed from outside of a class instance. Private variables can only be accessed by the class instance itself.
<?phpclass MyClass{   public $public = 'Public';   protected $protected = 'Protected';   private $private = 'Private';   function printHello()   {       echo $this->public;       echo $this->protected;       echo $this->private;   }}$obj = new MyClass();echo $obj->public; // Worksecho $obj->protected; // Fatal Errorecho $obj->private; // Fatal Error$obj->printHello(); // Shows Public, Protected and Private?>

http://www.php.net/manual/en/language.oop5.visibility.php

Link to comment
Share on other sites

You can also have a destructor that gets executed when the instance gets deleted.
Oh? Why my @&£$*! book didn't tell me about destructors then?Well, thanks. :)
target tag is deprecated in Xhtml strict, but I use it, simply choose transitional if you choose to use it, I always go transitional, because theres no other way to open new windows except with javascript, and too much trouble, just a note.
So it is after all? :) I was so used of using XHTML Strict
Link to comment
Share on other sites

Oh? Why my @&£$*! book didn't tell me about destructors then?
Only PHP 5 has destructors, PHP 4 does not.

All right, I checked my @&£$*! book, it was teaching me PHP 4. :)Good I did download the latest version of PHP (v.5.1.2)
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...