Jump to content

should i use class or require an inc file?


lnthai2002

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
Link to comment
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
Link to comment
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.
Link to comment
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.
Link to comment
Share on other sites

  • 5 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.