Jump to content

Basic Php Constructs For Oop/defining Classes


Recommended Posts

Posted

The general form for defining a new class in PHP is as follows:

class myclass extends myparent{

var $var1;

var $var2="constant string";

function myfunc($arg1, $arg2){

[...]

}

[...]

}

The special form class, followed by the name of the class that you want to define. An optional extensional clause, consisting of the word extends and then the name of the class that should be inherited from. A set of braces enclosing any number of the variable declarations and function definitions. Variable declarations start with the special form var, which is followed by a conventional $ variable name; they may also have an initial assignment to a constant value. Function definitions look much like standalone PHP functions but are local to the class. Example:
<?php

class textboxsimple{

var $body_text="my text";

function_display()

{

print("<table border=1><tr><td>$this->body_text");

print("</td></tr></table>");

}

}

?>

This is an extremely simple class definition. It has no parent(and, therefore, no extends clause). It has a single memebervariable(the variable $body_text), and a single member function(the function display()).The display function simply prints out the text variable, wrapped up in an HTML table definition.

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.