When your create a function within a class with the same name as the class that function'll execute whenever you create a N object of that class. This is called a ' constructor. ' It allows me to have default values for the each attribute automatically whenever I create an object:
<?php $Basic = new Style;?>
Define an instance of a class by giving it a name ($Basic) and assigning it "=new ClassName;".
You could also send different values for the variables as arguments when declaring the new Style, but if you declare a Val UE You are have to declare all of them which occur to the "right of" the one you declare (class functions work-like just Functions in this respect). Meaning if you are set ' text ' to something different than the default given in Style, then your have to declare all VARIABL Es. There is a easier way. We can create a function that changes a single variable within the class:
<?php
Function Set ($varname, $value) {
$this-> $varname = $value;
}
?>
So to change the value of particular variable of a instance we can:
<?php $Basic->set (' Size ', ' 2 ')?>
Use the '-> ' operator to refer to a variable or a function of the instance. So the above tells the interpreter "Run function ' Set () ' of instance ' $Basic '." It knows that "$Basic" is a instance of class "Styles" because we declared it so. Similarly we can refer to variables of a instance in same manner (e.g. $Basic->text).
Let's create a Style for table headers that has some slightly different attributes.
<?php
$Theader = new Style;
$Theader->set (' text ', ' #0000FF ');
$Theader->set (' Bgcol ', ' #000000 ');
?>
There, that ' s good enough. Now I table header has blue text on a black background. I want my table body to being a slightly lighter gray than my main page, black are good for text, but maybe I ' ll make the text Smaller:
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.