TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Request : OOP Help (http://www.talkphp.com/general/47-request-oop-help.html)

crash 05-05-2005 11:44 PM

Request : OOP Help
 
I am trying to learn OOP with PHP4, however my book dosnt explain it well - It just gives you alot of examples of OOP scripts without explaining anything.

So could someone make an example of an OOP script and explain everything (that includes $this-> whatever that does :)).

CreativeLogic 05-06-2005 05:49 AM

Are you looking for PHP help in PHP4 or PHP5?

zeromancer 05-08-2005 01:21 AM

OOP was completely redesigned in PHP 5. I would suggest upgrading to the most current version so you don't have to completely relearn everything when you move to PHP 5. The object structures were redesigned to have a much more java feel to them, complete with contructors and destructors, autoload, and much more predicatable behavior. but a VERY short tutorial follows for php 5:
PHP Code:

//class file
class myClass {
  
__constructor myCLass() {  //a constructor is executed when the class is initialized.
    
$this->name "default name";
  }

  function 
setName($newName) {
    
$this->name $newName;  //$this is a variable that should be used
  
}                           // when calling functions or variables from
                                         // within the class itself.
  
function getName() {
    return 
$this->name;
  }

  
__destructor killClass() {
    echo 
"Class is being destroyed.";
  }
}

//Inside your script

include("myClass.php");
$nameClass = new myClass(); //Initializes your new class.

echo $nameClass->getName(); // echos "Default Name"

$nameClass->setName("Steve");  //changes the name inside the class

echo $nameClass->getName();  // echos "Steve"

$nameClass->killClass();  //removes the class object from memory. 

I should be around to help if you have any other questions. I'm in the process of learning the new PHP5 oop structure myself. luckily i have a pretty good book on the subject.


All times are GMT. The time now is 04:13 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0