Back | Reverse | Quick Reply | Post Reply |

Object Oriented PHP
Link | by OneOne on 2010-03-12 09:37:04
Hello,

one of my hobbies is web programming in php, and I've made a couple sites using
it, one time a friend tells me is that I should be more object oriented or use
classes on my code, I use a lot of functions for re-usable code and I tried
reading some info about php classes,

looks to me classes are like functions V2.0 and cant get the logic behind it very
well, can anybody help to find some good 'for dummies' documentation on php
classes? I would hate to apply for a programming job and lose it because I don't
know how to implement classes.

if 2=1 then 2+2=?

Re: Object Oriented PHP
Link | by Vlad on 2010-04-03 10:27:10 (edited 2010-04-03 10:28:36)
This is a simple class, you create it by doing "$myclass = new Test();" and if you like to include arguments you can, default otherwise is the time when you create the class. Calling "$myclass->getDiff();" would return the time difference between the creation of the class and when you called the command. It's a simple example how you craft a class in PHP. You use $this to refer to the class inner variables and functions, you use like in Java private/public for variables and functions. For example you could create a class "Database" that handles all MySQL or what ever related tasks, so in the end you simply do "$db = new Database(); $res = $db->query("SELECT * FROM `tablename`");" and $res would contain True/False or a array with the actual data (or both), many possibilities and this way you don't have to do the whole MySQL query procedure, you shorten a lot of work.
class Test {
  private $start;
  private $end;
  public function __construct($time=time()) {
    $this->start = $time;
  }
  public function getDiff() {
    $this->end = time();
    return $this->end - $this->start;
  }
}

(if 2=1 then 2+2=2 because 2+2 is the same as 1+1 and since 2=1 then the result is "2")

Re: Object Oriented PHP
Link | by ve_krin on 2010-04-14 06:47:10
!OOT Alert!

(if 2=1 then 2+2=x
// What is x you asked? Doesn't matter what x is, since (2=1) will return FALSE, the program will skip any logic written inside the IF statement. ;p
// but anyway you can still put the variable x there, assuming its an integer, and x will have value of 4, not 2.)

Dream ends when we wake up and life ends when we die.
What's the different?

Re: Object Oriented PHP
Link | by OneOne on 2010-06-08 09:59:38
Thank you guys for the info about classes, I'll be trying gradually to migrate my webpages from full functions to classes and post the results.

about my signature, it was a logic game a teacher played on us at school

if 2=1 2+2=?

2=1 => 1+1 => 2=1 answer=1

kind of paradogic if you try to look at it from a programmer point of view.

if 2=1 then 2+2=?

Back | Reverse | Quick Reply | Post Reply |

Copyright 2000-2024 Gendou | Terms of Use | Page loaded in 0.0024 seconds at 2024-05-16 20:23:53