Hi,
I am keen to learn how to code classes in PHP. Can anybody recommend any good websites/books?
Hi,
I am keen to learn how to code classes in PHP. Can anybody recommend any good websites/books?
hey... here's a quick start-up example...
<?php
class connection
{
function connect()
{
$sql = mysql_connect("xxx","xxx","xxx");
}
}
?>
above... we made a class called : connection, and it contains one method ( one function ) which is : the connection function... now the class itself is nothing... so how do we create objects ? ( an object is an instance of a class... )... here's an example on how to access the above connection function of our connection class :
<?php
// the above class is included...
$obj = new connection;
$obj -> connect();
?>
and bingo, a database connection was established... hope the above simple example will help you to continue on...
for more resources about classes, click here and here
Good luck...
I personally like the book by Matt Zandstra titled PHP Objects, Patterns and Practices. It's not a book to learn classes, but it will teach you the basics of classes and inform you of some patterns that arise that you might be able to use. It's a very good book.
Any PHP5 book should go over some basic class construction and use stuff. But I don't know of any real strictly classes php book.
I think I understand. Can you give me an example, on how you would create a new class of dog, that inherits from a dog class?
The PHP Manual gives some information on this and some simple examples.
Here's a simple example:
class Animal
{
protected $legs;
protected $voice;
protected $hair;
public function __construct() {}
public function hasLegs($legs=2)
{
$this->legs = $legs;
}
public function setVoice($voice)
{
$this->voice = $voice;
}
public function setHair($hair=body)
{
$this->hair = $hair;
}
public function sayHello()
{
echo $this->voice;
}
}
The above is our parent class. Now we can extend it like so:
class dog extends Animal
{
public function __construct()
{
$this->setVoice('Woof Woof!');
$this->setHair('All Over');
$this->legs(4);
}
}
So then you can do:
<?php
$dog = new dog();
$dog->sayHello();
If you pick up a book or two, they have plenty of examples on this.
Should this:......
class Animal { protected $legs; protected $voice; protected $hair; public function __construct() {} public function hasLegs($legs=2) { $this->legs = $legs; } public function setVoice($voice) { $this->voice = $voice; } public function setHair($hair=body) { $this->hair = $hair; } public function sayHello() { echo $this->voice; } }
...this:
class dog extends Animal { public function __construct() { $this->setVoice('Woof Woof!'); $this->setHair('All Over'); $this->legs(4); } }
.....and this:
<?php $dog = new dog(); $dog->sayHello();
Be put in separate files?
Also, do you know of any good websites, where I can learn more?
Do you know of any good websites, where I can learn more?
Someone recommended Object Mentor, and I believe I have come across some of their articles when searching for online OO references. Take a look at their published articles, in particular the sections concerning Design Patterns, Design Principles, and Object Oriented Design.
Of course, for syntax read the PHP manual, as recommended by illzz.
If you are new to the world of object-oriented programming, I would recommend reading through Sun.com's Object-Oriented Programming Concepts tutorial. This gives a good background on the "why", then you can dive into the PHP manual and the various other PHP-specific references above to learn the "how".
bpat1434 wrote:I personally like the book by Matt Zandstra titled PHP Objects, Patterns and Practices. It's not a book to learn classes, but it will teach you the basics of classes and inform you of some patterns that arise that you might be able to use. It's a very good book.
I second this recommendation. That is a great book and it taught me a ton about object oriented programming.
Check this out PHP 101 from Zend http://devzone.zend.com/article/638-PHP-101-part-7-The-Bear-Necessities