Jump to content


should i use class or require an inc file?

  • Please log in to reply
5 replies to this topic

#1 lnthai2002

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 05 July 2006 - 04:25 AM

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

#2 trq

  • Staff Alumni
  • Advanced Member
  • 31,041 posts

Posted 05 July 2006 - 04:44 AM

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.

#3 lnthai2002

  • New Members
  • Pip
  • Newbie
  • 2 posts

Posted 13 July 2006 - 02:22 AM

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

#4 hvle

  • Members
  • PipPipPip
  • Advanced Member
  • 667 posts
  • Locationmelbourne, Australia

Posted 13 July 2006 - 02:37 AM

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:

// 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.
Life's too short for arguing.

#5 gewthen

  • Members
  • PipPip
  • Member
  • 11 posts

Posted 13 July 2006 - 02:51 AM

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!

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 autoloader 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:

class DataSack{

   public static function max($num1, $num2){ 
      if($num1<$num2) return $num2;
      return $num1; 

Now with the autoloader you can do:

echo DataSack->max(10, 12);

For example sake, it'll produce:


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.

#6 ShogunWarrior

  • Members
  • PipPipPip
  • Advanced Member
  • 528 posts
  • LocationIreland

Posted 03 January 2007 - 02:21 PM

You could just execute the function statically in both PHP 4/5 though:

<a href="http://www.daviddora...nmedia.com/">My New Site/Blog</a> | <a href="http://www.daviddora...m/check/">Check your page for broken links/images/scripts</a>

Zend Certified Engineer
Follow me on Twitter: http://twitter.com/davidd

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users