Jump to content

Archived

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

lnthai2002

should i use class or require an inc file?

Recommended Posts

Hi guys,
I am new to php programming. I read some php book and know that i can write a class similar to java in php and save it as an inc file to instantiate latter, but i can use the require directive to load a file to the body of the php file. So the question is that which way is better in practice in order to reuse code (modularize code)?
Hope to hear some suggestion.
Thanks in advance
Thai

Share this post


Link to post
Share on other sites
Your talking about two completely different things. You create a class, put it in a file, then call require() to include that file (and the class it contains) in another file.

Share this post


Link to post
Share on other sites
Hi,
Thanks for your explaination but i still dont understand completly.
I can write some function (without class definition) and put in a file then call require() to include that file in other file to use it.
With class definition, i HAVE to do the same thing except that i HAVE to instantiate the class which has been included before i can  use it! So what 's the point of using object oriented programming in web programming? Extra work, the same functionality and same data need to be sent over network(assumming apache server is on a different machine than the php files, thus, in order to process a completed php file, apache must fetch the class file and the main php file which uses the class!)
Hope anyone can clearify my concern
Thanks in advance

Share this post


Link to post
Share on other sites
you write bunch of functions and save it as functions.php
you write one class and save as class1.php

now in a script, you'll require:
require_once('functions.php');
require_once('class1.php');

// you can call a function here

// you can instantiate a class here:
$myclass = new class1();

class1.php is only definition.  If you like instatiate the same class in side the class1.php, it's your choice, but most people instantiate a class object inside the script unless they have a good reason to do so.

Share this post


Link to post
Share on other sites
[quote author=lnthai2002 link=topic=99477.msg396088#msg396088 date=1152757340]

With class definition, i HAVE to do the same thing except that i HAVE to instantiate the class which has been included before i can  use it!
[/quote]

Not true. I assume that you are used to putting one class per file in the java style. You can do the same thing with php, but it require you use the [url=http://www.php.net/manual/en/language.oop5.autoload.php]autoloader [/url]functionality. The autoloading allows you forget to include class files explictly. Once the class is included you do not have to instantiate the class to use it. You can call static methods on it. Example:

[code]
class DataSack{

  public static function max($num1, $num2){
      if($num1<$num2) return $num2;
      return $num1;
  }
}
[/code]

Now with the autoloader you can do:

[code]
echo DataSack->max(10, 12);
[/code]

For example sake, it'll produce:

[code]
12
[/code]

I hope this addressing your problem of not being able to use a class without instantiating the class. I assumed you are working with PHP 5. If you are stuck with PHP 4, I suggest forgetting about using object and use procedural programing.

Share this post


Link to post
Share on other sites
You could just execute the function statically in both PHP 4/5 though: [code] DataStack::max(10,12);[/code]

Share this post


Link to post
Share on other sites

×

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.