Forum Moderators: coopster
If anyone has any links to free resources or something that'll help me get my head around OO code for PHP, particularly using multiple cooperating classes I would be eternally grateful!
(also love to hear from anyone who's gone through the same learning curve. I've only ever used generic classes that have no interaction and my knowledge is letting me down a little!)
Thanks in advance :)
[edited by: ironik at 5:19 am (utc) on Mar. 29, 2005]
<?php
class Student {
var $name, $m1, $m2, $m3, $m4, $m5;
function setVal($value, $m1, $m2, $m3, $m4, $m5) {
$this->name=$value;
$this->m1=$m1;
$this->m2=$m2;
$this->m3=$m3;
$this->m4=$m4;
$this->m5=$m5;
}
function printName() {
echo $this->name;
echo "<br>";
}
function calAverage() {
echo ($this->m1+$this->m2+$this->m3+$this->m4+$this->m5)/5;
echo "<br><br>";
}
}
$s1=new Student;
$s1->setVal("Mohit", 52, 36, 70, 63, 45);
$s1->printName();
$s1->calAverage();
$s2=new Student;
$s2->setVal("Rohit", 51, 36, 67, 43, 35);
$s2->printName();
$s2->calAverage();
$s3=new Student;
$s3->setVal("Pankaj", 61, 56, 76, 53, 55);
$s3->printName();
$s3->calAverage();
?>
<?php
class Connection {
var $host, $user, $password;
function initialize($h, $u, $p) {
$this->host=$h;
$this->user=$u;
$this->password=$p;
}
function connect() {
mysql_connect($this->host, $this->user, $this->password);
echo "Successfully connected to MySQL database Server.";
}
}
$conn=new Connection();
$conn->initialize('localhost', 'admin', 'admin');
$conn->connect();
?>
I'm subscribed to phpclasses already, great site to find classes without having to 'reinvent the wheel'.
Last night I tested out my code based on my understanding of the MVC pattern. To my surprise after only 1/2 hour of debugging it was working pretty smoothly, I have about 10 classes that are working harmoniously together... there will probably be a few more when I manage to finish it.
Thanks again