3 Replies - 1625 Views - Last Post: 03 January 2012 - 01:42 PM Rate Topic: -----

#1 CarDriver   User is offline

  • D.I.C Regular
  • member icon

Reputation: 65
  • View blog
  • Posts: 431
  • Joined: 17-February 11

When is OO used in PHP for web programming? It's incredibly diffic

Posted 29 December 2011 - 09:05 PM

I'm beginning to think the object oriented aspect of PHP is just a nice little extra feature for people who use PHP for general purpose programming. I don't see how OO fits in to web programming (mostly because it seems impossible to use).

Still, I tried writing a class in order to use a function from a script in a different file, but that didn't work. I've been using include and require to access variables from other classes, but the second I tried using a function in the same manner I was yelled at with errors. So, help me out and tell me why something like the following doesn't work.


class testClass {
     function testMethod() {
          echo ("Why doesn't this work?");



include 'classFile.php';

$classInstance = new testClass();


Apparently doing something like this is the same as creating two identical classes, and I don't see why. So please, tell me the correct way to use the basics of classes, objects, and functions in PHP. Is it unheard of (or impossible?) to simply have a nice directory of .php files that contain functions that other scripts can access with ease? Now that I think of it, I guess I see why classes might be handy (and in some cases essential) for web programming with PHP, but at this point, despite having no problem with the concepts or syntax, I cannot figure out how to use them.

Thank you!

EDIT: I just watched a video that addressed some of my issues, and one of the problems was that my syntax was incorrect, actually. So I understand how to make an example like the one above work, but I would still like some information on why OO is used in PHP and how to properly use include. I'm still a bit lost...

This post has been edited by AVReidy: 29 December 2011 - 09:23 PM

Is This A Good Question/Topic? 0
  • +

Replies To: When is OO used in PHP for web programming? It's incredibly diffic

#2 macosxnerd101   User is online

  • Games, Graphs, and Auctions
  • member icon

Reputation: 12317
  • View blog
  • Posts: 45,416
  • Joined: 27-December 08

Re: When is OO used in PHP for web programming? It's incredibly diffic

Posted 29 December 2011 - 11:43 PM

My tutorial on OOPHP might be a good place to start. OOP is used in any language because it is a good tool for modeling data. Encapsulation is the big one. In many cases, OOP is cleaner than using the nested arrays you will commonly see used in PHP. Based on your signature, it looks like you are used to working with languages like Java and C#, which force OOP on the developer. A lot of the same design patterns apply with PHP.

As for the include statement, check out the manual.
Was This Post Helpful? 2
  • +
  • -

#3 jkeilman   User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 04-March 10

Re: When is OO used in PHP for web programming? It's incredibly diffic

Posted 03 January 2012 - 09:27 AM

OO is used in PHP for the same reasons you would think of using OO in any other programming language. With the rise of the Internet comes more and more web applications that should be able to take advantage of OO. If a programmer is coding a website where you can say, play a card game, and that programmer is using PHP, why wouldn't he want to create a "card" object? I believe in many cases, language becomes irrelevant (I say that loosely), and it is all a matter of using what you know best to solve a problem. That being said, I realize some languages are better at doing certain things than others but, I think you understand what I mean.
As far as the include statement goes, you will obviously have a page consisting of nothing but functions. Let's say this page is called functions.php.

    function addition($x, $y) {
        $sum = $x + $y;
        return $sum;

You will then include this functions page in your content page and call upon the functions from within your content page, let's call it content.php.

    include("[path to content.php]"); /* you may want to consider using require_once, as  this will be essential */
    // this will set a variable, $sum, equal to the value returned in our function
    $sum = addition(3,4);
    echo $sum;

This post has been edited by jkeilman: 03 January 2012 - 09:32 AM

Was This Post Helpful? 1
  • +
  • -

#4 e_i_pi   User is offline

  • = -1
  • member icon

Reputation: 879
  • View blog
  • Posts: 1,893
  • Joined: 30-January 09

Re: When is OO used in PHP for web programming? It's incredibly diffic

Posted 03 January 2012 - 01:42 PM


In the past year I switched to OO, throwing out all my old code and rewriting it from scratch. The result? Hundreds more files, but strangely enough, it's much easier to manage. Also, I have a fair amount of code reuse, and having now created my own OO framework and become familiar with it, it becomes much easier to write.

As an example, this is what my old code looked like:

...and this is the equivalent in OO...

The second lot of code is much easier to:
  • Understand
  • Debug
  • Extend
  • Reuse

What my application is doing right now would be a nightmare with modular code (which is what I had before). I was running into dozens of terrible situations to do with scope, code bloat, dead code, separation issues, and refactoring.

For example, if I want to create a new part of a page with my OO framework, I have to create two files: the Logic file and the Presentation file. If the part is a user interface, I have to create three files: the Logic file, the Presentation file (HTML) and the Presentation file (JS). If it interacts with the database, I can use an existing Data file, or create a new Data file.

In terms of includes, using an autoloader with an accompanying namespace and directory structure makes life a lot easier. You achieve code separation both on a file content level and a file location level. There'a a tutorial right here on DIC if you want to read up about namespacing and autoloading.

Also, using a front contoller can make life easier. I have a front controller that calls class/methods rather than files, here's a stripped back version of it:
// Include the configuration file

// Get the script variables
$param_class = $_POST['param_class'];
$param_method = $_POST['param_method'];

// Get the server data
if (!isset($_POST['param_server_data']))
	$param_server_data = array();
} else {
	$param_server_data = json_decode($_POST['param_server_data'], true);
// Append any client data to the server data
$param_client_data = json_decode(stripslashes(urldecode($_POST['param_client_data'])));
	$param_server_data['ClientData'] = get_object_vars($param_client_data);
} else {
	$param_server_data['ClientData'] = array();
// Append any attached files to the client data
	$param_server_data['Files'] = $_FILES;

// Commit the script action
$script = new $param_class();
echo $script->$param_method($param_server_data);

Hope this helps explain a little about why OO can be beneficial in PHP web programming :)
Was This Post Helpful? 7
  • +
  • -

Page 1 of 1