Everything OOP starts with classes. Classes are the type of objects, in the same way that integer may be the type of variable. You have to create the type before you can create specific objects of that type. That's important to realize -- a class is an object type. And before you can structure objects as you like, you can then create object of that class, as you are going to see.
class Person
{
.
.
.
}
===========================================
okay, that creates a class named Person. In PHP, classes are usually given names that start with a capital letter, and objects are given names that start with a lowercase letter. As you know, classes can hold data items, called properties, so let's give the person a name, $name. In the class, you're declaring the properties and methods that will go into the objects of this class- and that means you use the var statement to declare properties in PHP. Here's how you add a property named &name to the Person class;
class Person
{
var $name;
.
.
}
================================================
we can add methods to Person class.
class Person
{
var $name;
function set_name($data)
{
.
.
}
}
=================================================
Now you've got to store the name passed to the set_name function in the $name variable. You could do that, by accessing $name as a global variable;
class Person
{
var $name;
function set_name($data)
{
global $name;
.
}
}
======================================================
Then you can assign the argument passed to set_name, $data to the internally stored name, $name, like this;
class Person
{
var $name;
function set_name($data)
{
global $name;
$name=$data;
.
}
}
=============================================================
That stores the person's name. You'll also need some way to read the person's name, so you might add another method called get_name;
class Person
{
var $name;
function set_name($data)
{
global $name;
$name=$data;
.
}
function get_name()
{
.
.
}
}
====================================================================
{
var $name;
function set_name($data)
{
global $name;
$name=$data;
.
}
function get_name()
{
.
.
}
}
====================================================================
In this get method, you can access the internal name, $name, and return it like this .
class Person
{
var $name;
function set_name($data)
{
global $name;
$name=$data;
.
}
function get_name()
{
global $name;
return $name;
}
}
==================================================================
that look goods, and it will work, but there's one change to make. You won't normally see OOP done using the global keyword. Instead, you usually refer to the properties tof the class using the $this keyword. The $this keyword points the current object. In PHP terms, this;
global $name;
$name=$data;
can replace by this;
$this->name=$data;
===================================================================
note the syntax here you use $this, followed by the -> operator. In addition, you omit the $ in front of the property you're referring to, like this; $this->name.
class Person
{
var $name;
function set_name($data)
{
$this->name=$data;
.
}
function get_name()
{
return $this->name;
}
}
okay you've now created PHP Person class. Now it's time to put it to work, creating some objects.
==============================================================
Creating Objects
to create new object, you use the new operator in PHP. you just need to define your class and then you can use the new operator to create an object that class. Objects are stored as variables, so the object-creation process using new operator looks like this.
class Person
{
var $name;
function set_name($data)
{
$this->name=$data;
.
}
function get_name()
{
return $this->name;
}
}
$raj=new Person;
Great, That's created a new person object named $ralph. Easy.
All the methods and properties in the Person class are built into the $raj object. So how can you call the set_name method like this, using the -> operator again;
$raj=new Person;
$raj=set_name("Ramesh");
=======================================================================
example 1
<?php
class dog {
var $name;
var $age;
var $owner;
function dog($in_name="Rajesh",$in_age="1",$in_owner="Rajtutorial") {
$this->name = $in_name;
$this->age = $in_age;
$this->owner = $in_owner;
}
function getage()
{
return ($this->age);
}
function getowner()
{
return ($this->owner);
}
function getname()
{
return ($this->name);
}
}
$raj= new dog;
echo $raj->getage();
echo "<br>";
echo $raj->getname();
echo "<br>";
echo $raj->getowner();
?>
===========================================
Example 2.
<?php
class human {
public $gender='Male';
}
$Johnny = new human;
echo 'Johnny is a '. $Johnny->gender.'.';
<?
================================================
<?php
class human {
public $gender;
public function __construct($gender)
{
$this->gender = $gender;
echo self::get_gender();
}
public function get_gender()
{
return $this->gender;
}
}
$Johnny = new human('male');
===============================================
Objects
The first thing that we need to wrap our heads around is the principle of Classes. Put simply, a Class is an Object, and an Object is a Class. But, like an everyday object, PHP Objects have certain properties, and they can do things. A simple class might look like this:
Example 2.
<?php
class human {
public $gender='Male';
}
$Johnny = new human;
echo 'Johnny is a '. $Johnny->gender.'.';
<?
================================================
<?php
class human {
public $gender;
public function __construct($gender)
{
$this->gender = $gender;
echo self::get_gender();
}
public function get_gender()
{
return $this->gender;
}
}
$Johnny = new human('male');
?>
===============================================
No comments:
Post a Comment