The Basics of PHP Objects

The Basics of PHP Objects

Naming

Classes :
The first letter of a class name is always capitalized, the rest of the letter is always lowercase. Should the name of several words exist then they are separated by an underscore. The first letter of successive words is also capitalized.

Functions and Methods :
Function names may only contain alphanumeric characters. Underscores are not permitted.

Function names must always start with a lowercase letter. When a function name consists of more than one word, the first letter of each new word must be capitalized. This is commonly called "camelCase" formatting.


Variables:
Variable names may only contain alphanumeric characters. Underscores are not permitted. Numbers are permitted in variable names but are discouraged in most cases.   

For instance variables that are declared with the "private" or "protected" modifier, the first character of the variable name must be a single underscore. This is the only acceptable application of an underscore in a variable name. Member variables declared "public" should never start with an underscore.


Constants:
Constants may contain both alphanumeric characters and underscores. Numbers are permitted in constant names.

All letters used in a constant name must be capitalized, while all words in a constant name must be separated by underscore characters.

Filenames:
For all other files, only alphanumeric characters, underscores, and the dash character ("-") are permitted. Spaces are strictly prohibited.

 


Create class
# class_lib.php

class Company {
// declare variable
  var $name = "Video Data Software Z";
  function get_name() {      // create function / method inside class Company
      return $this->name;    // return  (this ( this oblect)) -> (variable) name
   }
}



# index.php

<?php include("class_lib.php");?>
<html>
..
<body>

<?php
$MyObject = new Company();  // create new object ($MyObject) from class Company
echo $MyObject->get_name();  // use object ($MyObject) from class Company and call function get_name from this class (Company)

?>
</body>
</html>



output result:  Video Data Software Z
 


Use a function from other class
First create 2 class files Car_class.php  CarColor_class.php and index.php
# CarColor_class.php

<?php
class CarColor {
  function get_CarColor() {
     echo "black";
  }
}
?>

# Car_class.php

<?php
include("CarColor_class.php");  // include class funtions what will be used in the class car
class Car {
  function get_Car() {      // create function / method inside class car
      echo "Renault: ";
     // use now a function from other class (CarColor_class.php)
     $MyCarColor = new CarColor();  // create new object ($MyCarColor) from class CarColor
     echo $MyCarColor->get_CarColor();
   }
}
?>


# index.php

<?php include("Car_class.php");?>
<html>
..
<body>

<?php
$MyCar = new Car();  // create new object ($MyCar) from class Car
echo $MyCar->get_Car();  // use object ($MyCar) from class car and call function get_Car
?>
</body>
</html>


output result: 
Renault: black
 


Tutorials :
net.tutsplus.com Object-Oriented PHP for Beginners 
net.tutsplus.com Real-World OOP With PHP and MySQL