It is time to start with OO

2 views
Skip to first unread message

Maciej

unread,
Nov 10, 2009, 5:46:49 AM11/10/09
to PHPNW
Hello everyone.

I believe there are some people with more experience in PHP then me.
Last few months I was working on quite big project based on PHP/MySQL
but because my knowledge is based on some ancient book PHP4/5 it
think I could do it better. I mean - it does work - however I'm
getting a little bit lost. For example the file that contains
functions is now more than 1200 lines and 40kB in weight and it's
still more things to come.
When I was searching for easy approach to learn OO style programming I
didn't have that experience which I've got now and now I understand
why its good way to programing this way ... or at least easier to
read own codes from let say 4 months ago.
Can someone please help me to start somehow.
Let say for example I need to display something from database.
Table looks like:

Acc.no. Name Address Type of Acc Email
1 John 4 Marketstreet 5
jo...@serv.com
2 Sue 5 Clogh Avenue 3
s...@serv.co.uk

So far, when I need to display something I'm using
$query=mysql_query(select from ...
$row =mysql_fetch ...
echo $row[Name] ...
and so on ...

Question is - how can I do this other way round - like
echo this.Accno=>1.name
echo this Accno=>2.Address
and so on ...

I know my explanation may not be perfect, but if you can find couple
of minutes to tell me at least where I can find exact solution to
my ... issue.

Regrds - Matt.

P.S. Sorry for the language - I am a foreign person and still working
on my English.

Cheers.

Shaun Brown

unread,
Nov 10, 2009, 6:14:04 AM11/10/09
to ph...@googlegroups.com
Hello Matt,
I've been programming in OO PHP5 for about 2 years now, although I've no experience in working with it, I do it in my spare time =]
 
To achieve what you're wanting to get in OO PHP5 I'd do the following:
 
// contains all the methods, each time the method is called the default initialisations in the constructor are done...
class Example
{
    protected $connection; // using protected because if we want to extend this class, we can use this variable, if it was private, only this instance of the class can use it.
 
   public function __construct()
   {
       $this->connection = new MySqli("localhost", "root", "password", "database"); // create a new instance of the new mysqli object, with the appropiate connection string needed.
   }
 
   // this is fired once the instance is disposed of, a pretty cool/important magic method :)
   public function __destruct()
   {
      $this->connection->close(); // kill the connection once the instance is disposed of.
   }
 
   public function ExampleQuery()
   {
       $sql = "SELECT `Acc.no`, `Name`, `Address`, `Type`, `etc...` FROM `table`;";
       $query=$this->connection->query($sql);
 
      // check for rows in the query object =]
      if($query->num_rows > 0)
      {
         // lets get a mysqli accociate array of the results
         while($sqlData=$query->fetch_array(MYSQLI_ASSOC))
         {
              print $sqlData["YourFields in here..."] . "<br />\n";
         }
         // free up the memory of the query :)
         $query->free();
      }
   }
}
 
 
//then anywhere the class is included, you create a new instance of it like so:
$example = new Example();
// call a public method within the instance.
$example->ExampleQuery();   // this prints out all the query results coded in the above method ^^
 
 
I hope this helps Matt, I'm more than happy to help you in anything else.
 
PS, sorry for the crappy indentation, I'm using hotmails notepad, due to lack of tools in college :(
 
Shaun


 
> Date: Tue, 10 Nov 2009 02:46:49 -0800
> Subject: [phpnw] It is time to start with OO
> From: mleg...@googlemail.com
> To: ph...@googlegroups.com

Joseph Edmonds

unread,
Nov 10, 2009, 6:22:44 AM11/10/09
to ph...@googlegroups.com
http://pastebin.com/ is great for this kind of thing

2009/11/10 Shaun Brown <shaun_b...@msn.com>



--
Cheers...

jOSEPH

www.edmondscommerce.co.uk



Neil Bellamy

unread,
Nov 10, 2009, 6:01:09 AM11/10/09
to ph...@googlegroups.com
Hey Matt

Try this :

http://buildinternet.com/2009/07/an-introduction-to-object-oriented-php-part-1

Most introductions to OO are full of unusual language - this one seems easier than most to follow.

Good luck with it !


  

Thyberthpithe

unread,
Nov 10, 2009, 7:22:34 AM11/10/09
to ph...@googlegroups.com
Hi,

On 10 Nov 2009, at 10:46, Maciej wrote:

[snip]

> When I was searching for easy approach to learn OO style programming I
> didn't have that experience which I've got now and now I understand
> why its good way to programing this way ... or at least easier to
> read own codes from let say 4 months ago.

> Can someone please help me to start somehow.

[snip]

Various people have given you links that teach OO programming in PHP. I strongly suggest you should look at some of the theory behind Object Orientation and learn some of the terminology. Specifically look at design patterns http://en.wikipedia.org/wiki/Design_pattern_(computer_science)

OO can be a powerful tool in the programmers arsenal. But it can be a can of worms too. Some of the worst code I have ever seen is poorly written OO code. It was a convoluted mess and extremely difficult to maintain. You don't need an OO language to write OO and similarly you aren't necessarily writing OO when you're using an OO language. OO languages make it easier to write OO but are not a requirement.

Take time. Think about your design. And have fun.

Melanie

Shaun Brown

unread,
Nov 10, 2009, 7:34:31 AM11/10/09
to ph...@googlegroups.com
Very good point Melanie!
 
Definately would have to agree, When I started learning OO languages such as C++, C# coming from just scripting languages in the likes of PHP4, and Pascal.
 
I didn't understand, and quite honestly still don't understand the best and good practices of OO programming, main reason for me being at college ^^
 
I find from my own experience of learning programming languages in my spare time, that you don't learn the reason behind them, and the best practices.
 
Really good advice again,
Shaun
 
> Subject: [phpnw] Re: It is time to start with OO
> From: thyber...@gmail.com
> Date: Tue, 10 Nov 2009 12:22:34 +0000
> To: ph...@googlegroups.com

Maciej

unread,
Nov 10, 2009, 7:51:38 AM11/10/09
to PHPNW
Thank you to everyone for advise.
I'll sit on that after work - now I've got just bit of free time.

I'm still kind of confused ... this project I'm working on now, from
the programing point of view it isn't "rocket science". It's got only
5 type of users - those guys has got access to designated for them
data ... and that's it.
Structural way it's done so far works fine, but I know in the future
if any changes, this will be royal pain in neck to add/change
something.

Thanks again - Have a good day - Matt.

On Nov 10, 12:34 pm, Shaun Brown <shaun_brown...@msn.com> wrote:
> Very good point Melanie!
>
> Definately would have to agree, When I started learning OO languages such as C++, C# coming from just scripting languages in the likes of PHP4, and Pascal.
>
> I didn't understand, and quite honestly still don't understand the best and good practices of OO programming, main reason for me being at college ^^
>
> I find from my own experience of learning programming languages in my spare time, that you don't learn the reason behind them, and the best practices.
>
> Really good advice again,
>
> Shaun
>
>
>
> > Subject: [phpnw] Re: It is time to start with OO
> > From: thyberthpi...@gmail.com
> > Date: Tue, 10 Nov 2009 12:22:34 +0000
> > To: ph...@googlegroups.com
>
> > Hi,
>
> > On 10 Nov 2009, at 10:46, Maciej wrote:
>
> > [snip]
>
> > > When I was searching for easy approach to learn OO style programming I
> > > didn't have that experience which I've got now and now I understand
> > > why its good way to programing this way ... or at least easier to
> > > read own codes from let say 4 months ago.
>
> > > Can someone please help me to start somehow.
>
> > [snip]
>
> > Various people have given you links that teach OO programming in PHP. I strongly suggest you should look at some of the theory behind Object Orientation and learn some of the terminology. Specifically look at design patternshttp://en.wikipedia.org/wiki/Design_pattern_(computer_science)
>
> > OO can be a powerful tool in the programmers arsenal. But it can be a can of worms too. Some of the worst code I have ever seen is poorly written OO code. It was a convoluted mess and extremely difficult to maintain. You don't need an OO language to write OO and similarly you aren't necessarily writing OO when you're using an OO language. OO languages make it easier to write OO but are not a requirement.
>
> > Take time. Think about your design. And have fun.
>
> > Melanie
>
> _________________________________________________________________
> Have more than one Hotmail account? Link them together to easily access both
>  http://clk.atdmt.com/UKM/go/186394591/direct/01/

Maciej

unread,
Nov 10, 2009, 7:56:47 AM11/10/09
to PHPNW
Thank you to everyone for advise.
I'll sit on that after work - now I've got just bit of free time.

I'm still kind of confused ... this project I'm working on now, from
the programing point of view it isn't "rocket science". It's got only
5 type of users - those guys has got access to designated for them
data ... and that's it.
Structural way it's done so far works fine, but I know in the future
if any changes, this will be royal pain in neck to add/change
something.

Thanks again - Have a good day - Matt.

On Nov 10, 12:34 pm, Shaun Brown <shaun_brown...@msn.com> wrote:
> Very good point Melanie!
>
> Definately would have to agree, When I started learning OO languages such as C++, C# coming from just scripting languages in the likes of PHP4, and Pascal.
>
> I didn't understand, and quite honestly still don't understand the best and good practices of OO programming, main reason for me being at college ^^
>
> I find from my own experience of learning programming languages in my spare time, that you don't learn the reason behind them, and the best practices.
>
> Really good advice again,
>
> Shaun
>
>
>
> > Subject: [phpnw] Re: It is time to start with OO
> > From: thyberthpi...@gmail.com
> > Date: Tue, 10 Nov 2009 12:22:34 +0000
> > To: ph...@googlegroups.com
>
> > Hi,
>
> > On 10 Nov 2009, at 10:46, Maciej wrote:
>
> > [snip]
>
> > > When I was searching for easy approach to learn OO style programming I
> > > didn't have that experience which I've got now and now I understand
> > > why its good way to programing this way ... or at least easier to
> > > read own codes from let say 4 months ago.
>
> > > Can someone please help me to start somehow.
>
> > [snip]
>
> > Various people have given you links that teach OO programming in PHP. I strongly suggest you should look at some of the theory behind Object Orientation and learn some of the terminology. Specifically look at design patternshttp://en.wikipedia.org/wiki/Design_pattern_(computer_science)
>
> > OO can be a powerful tool in the programmers arsenal. But it can be a can of worms too. Some of the worst code I have ever seen is poorly written OO code. It was a convoluted mess and extremely difficult to maintain. You don't need an OO language to write OO and similarly you aren't necessarily writing OO when you're using an OO language. OO languages make it easier to write OO but are not a requirement.
>
> > Take time. Think about your design. And have fun.
>
> > Melanie
>

Robert Mortimer

unread,
Nov 10, 2009, 2:56:41 PM11/10/09
to ph...@googlegroups.com
2009/11/10 Maciej <mleg...@googlemail.com>:
>
> Thank you to everyone for advise.
> I'll sit on that after work - now I've got just bit of free time.
>
> I'm still kind of confused ... this project I'm working on now, from
> the programing point of view it isn't "rocket science". It's got only
> 5 type of users - those guys has got access to designated for them
> data ... and that's it.
> Structural way it's done so far works fine, but I know in the future
> if any changes, this will be royal pain in neck to add/change
> something.
>
> Thanks again - Have a good day - Matt.
>

Try POG
POG is the PHP object Generator. It provides you with nice objects
with save delete etc. with simple relations.
http://www.phpobjectgenerator.com/
Learn by example, look at the code, it's a really good intro without
the full mega framework.

Rob

Phil Moorhouse

unread,
Nov 11, 2009, 10:16:01 AM11/11/09
to PHPNW
Once you understand the basic syntax and concepts of OO PHP, take a
look at an existing PHP5 Framework (see http://www.phpframeworks.com/
for a list of the better-known ones).

I can't speak for all of them but I know that Symfony makes an effort
to push best practice development. Just by reading the Symfony book /
Jobeet tutorial you'll learn a lot about OO development, common design
patterns (MVC & ORM), unit testing, ajax, caching, performance etc.

Some general ideas for becoming a better developer:
* Subscribe to developer blogs, (even if they're not about PHP) so
that you get an idea of what's possible, it might not all be relevant
this minute but it'll be in the back of your mind should the need crop
up in future.
* Read lots of other peoples code, but don't take it for granted that
just because something is open source, it'll be well designed.
* Play with different languages / frameworks / tools as much as you
can, don't just save it for when you need to write a new project.

Finally, there's the old adage that if you don't cringe when you look
at your old code, then you've not progressed as a developer.

Apologies if I've gone off on a bit of a tangent there.

Phil

Retardo Farengae

unread,
Nov 13, 2009, 5:54:10 AM11/13/09
to ph...@googlegroups.com
Fixed some errors (im at work, was a bit rushed ;)
 
<?php
/**
 * @author Jason Leyland
 * @copyright 2009
 */
class myTable{
 /* Set up some variables */
 public $tableName;
 public $query;
 private $error;
 private $header = true;
 /* Access protected */
 function __construct(){
  /* This is simalar to document.onLoad */
 }
 public function getTable(){
  global $database;
  /* Our query string */
  $q = "select * from $this->tableName where $this->query";
  /* Execute our query using our database class */
  $result = $database->query($q);
  /* Set up our local return variable */
  $output = '<table>';
  /* Check the query hasn't failed */
  if($database->checkResult($result) !== false){
   /* Loop through our data */
   while($row = mysql_fetch_assoc($result)){
    $output .= '<tr>';
    foreach($row as $key => $value){
     /* Construct the table cells */
     if($header){
      $output .= "<th>$value</th>";
      $header = false;
     }else{
      $output .= "<td>$value</td>";
     }
    }
    $output .= '</tr>';
   }
   return "$output</table>";
  }else{
   $this->setError();
   return '<p>There has been an unexpected error: '.$this->error.'</p>';
  }
 }
 private function setError(){
  $this->error = mysql_error();
 }
 function __destruct(){
  /* This is simalar to document.onUnLoad */
 }
}
if(isset($_GET['myvariable'])){
 /* Initiate the class */
 $myTable = new myTable();
 /* Set up some variables */
 $myTable->tableName = 'sometable';
 $myTable->query = "myvariable = ".isset($_GET['myvariable']);
 /* Execute our query */
 echo($myTable->getTable());
}else{
 echo('<p>There has been an unexpected error</p>');
}

?>

On Fri, Nov 13, 2009 at 10:50 AM, Retardo Farengae <fare...@googlemail.com> wrote:
This is how I personally would do it in this slightly exaggerated example. Always more than happy to help o7 . I learnt oop when I started playing with Java however, PHP does it quite nicely these days.  Best thing for you to do is download some classes (http://www.phpclasses.org/) and implement them / see how they work. If this code needs further explanation just let me know.
 
<?php

/**
 * @author Jason Leyland
 * @copyright 2009
 */

class myTable{
 /* Set up some variables */
 public $tableName;
 public $query;
 private $error;
 private $header = true;
 /* Access protected */
 function __construct(){
  /* This is simalar to document.onLoad */
 }
 public function getTable(){
  global $database;
  /* Our query string */
  $q = "select * from $tableName where $query";
  /* Execute our query using our database class */
  $result = $database->query($q);
  /* Set up our local return variable */
  $output = '<table>';
  /* Check the query hasn't failed */
  if($database->checkResult !== false){
   /* Loop through our data */
   while($row = mysql_fetch_assoc()){
    $output .= '<tr>';
    foreach($row as $key => $value){
     /* Construct the table cells */
     if($header){
      $output .= "<th>$value</th>";
      $header = false;
     }else{
      $output .= "<td>$value</td>";
     }
    }
    $output .= '</tr>';
   }
   return "$output</table>";
  }else{
   $this->setError();
   return '<p>There has been an unexpected error: '.$this->error.'</p>';
  }
 }
 private function setError(){
  $this->error = mysql_error();
 }
 function __destruct(){
  /* This is simalar to document.onUnLoad */
 }
}
if(isset($_GET['myvariable'])){
 /* Initiate the class */
 $myTable = new myTable();
 /* Set up some variables */
 $myTable->tableName = 'sometable';
 $myTable->query = "myvariable = ".isset($_GET['myvariable']);
 /* Execute our query */
 echo($myTable->getTable());
}else{
 echo('<p>There has been an unexpected error</p>');
}


?>

Retardo Farengae

unread,
Nov 13, 2009, 5:50:44 AM11/13/09
to ph...@googlegroups.com
Reply all
Reply to author
Forward
0 new messages