|
Class: Explained : Always Simple |
The example below is a simple class with one constructor function; here
class name and function name are same as "A".
- Keyword : class and curly braces { }
- Benefit : ruse, encapsulation and inheritance
- No Pointer like C++
|
| |
<?php class A { public function A($param) { echo "Constructing A.$param<br>"; } } //calling classes, creating instances $a = new A(10); ?>

|
| |
| When you create an object by using a new instance of
the class ( that is an object). The example shown below, passes a
variable/parameter from a "Form" to an argument of function "anyfunction".
The symbol "->" in the code "$some_object->anyfunction($y);" , works like a
pointer and it can be used |
Code: pointer_to_variable.php<?php class myClass {
var $word = 'Hello Wrold';
function myClass(){ echo "Constructor <br>"; } function something($n) { echo "<br> Received : $n "; } } $some_object=new myClass(); echo $some_object->word; echo "<br>sending parameter 10 to function something "; echo $some_object->something(10); ?>

|
| Pointer to variables are not efficient, and
looked like they are loosely attached |
code:class_explined.php
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=windows-1252"> <title>class_explained</title> </head> <body text="#FFFF00" bgcolor="#000080"> <?php $y=$_POST['T1']; class myClass { var $word ='welcome'; function myClass(){ echo "An instance is created, I am a constructor <br>"; } function anyfunction($x){ echo "This function was called me as <font color='#FF0000'> $x </font>"; } } $mine=new myClass(); echo "<br>Calling a variable with <br>"; echo $mine->$word ; $mine->anyfunction($y);
?> <form method="POST" action="class_explained.php"> <p><input type="text" name="T1" size="20"><br> <input type="submit" value="Submit" name="B1"> <input type="reset" value="Reset" name="B2"></p> </form> </body> </html>


|
| |
|