Skip to main content

Class

1. Class

Before you begin to code you should make a plan, objected oriented programming is all about making a plan. A class is a plan. Now each of you try to think about making a plan following me. For instance my plan is about a cookie factory. I want to make a cookie factory in order to produce cookies for selling. Without thinking no more, I will create a class called Cookie:
<?php
class Cookie {
}
?>

2. Property

My cookies will have some features like color, weight, quantity, price. These features or properties are stored in variables. By the way, what is a variable?

2.1 Variables, Strings, Numbers

The variable is a place in computer’s memory where a value like string or number is stored.
<?php
$color = ’black’;
$color = ”red”;
$quantity = 3;
?>

Note that a variable in php always starts with $ sign. Also note that a statement ends with a semicolon. Strings (or letters) are always enclosed in single or double quotes.
So my class till now show as:
<?php
class Cockie{
public $color;
public $quantity;
}
?>

3. Method

Until now I created a class and properties but I need methods too. The method or function (function is another word for method) implies an action. In my case, I want to create a method by which the chef shouts that the cookies are ready:
public function chefShout () {
echo ”the cooooookies are ready, guys”;
}

3.1 Function Parameters

Function Parameters are important for a function because represents inputs for that function. Think about my cookies. The chef needs ingredients to be able to produce the cookies. He receives the ingredients outside the class without using class properties through parameters:
<?php public function buyIngredients($salt, $sugar, $flower, $oil ){
$total = $salt, $sugar, $flower, $oil;
return $total;
}
?>

Maybe you wonder what the expression return helps us with. Return means what the function produces. In my case when I will call my function makeCookie I will provide it as input values for flower, sugar, oil and salt and my function will provide me, will return to me as output with the result of a summing up. Be careful!!! Return expression completes code execution, the code following return expression will never be executing:
public function buyIngredients ($flower, $sugar, $oil, $salt) {
$total = $flower+$sugar+$ulei+$salt;
return $total;
echo ”Hello”; // the statement echo „Hello” after return statement is useless. }

3.2 Methods using class properties

As I said earlier in order for a method to output something it needs some value as input. In a class often the method uses the class properties. In order to use class properties you need the special expression $this→
public function buyIngredients ($flower, $sugar, $oil, $salt) {
$total = $this->flower+$this->sugar+$this->oil+$this->salt;
// $this->flower is nothing else but my property $flower defined inside the class, this time used inside the function return $total;
}

Let’s recap:
class Cockie{
public $flower;
public $sugar;
public $oil;
public $salt;
public function buyIngredients ($flower, $sugar, $oil, $salt) {
$total = $this->flower+$this->sugar+$this->oil+$this->salt;
return $total;
}
}

3.3 Structures inside the methods

Obviously, a method can be written in a more complicated way depending on my needs, using: - conditional structure if else;
- repetitive (iterative) structure for, while;
- mathematical operations (addition, substraction, multiplication and division)
The methods reflecting the thinking of the programmer, there is no standard recipe. Of course you should add as many properties and methods as you need, neither more nor less.

3.2 Arrays

Array is a list of values. It is important to retain that every value has its own position in the array. In real life unlike programming the first position is position 0.
$color = array("red", "blue", "pink", "green","orange","yellow");
green is on fourth position but its indexed is three as I said earlier the position starts with 0. If you want to go through this array we need to use for loop. for($i = 0; $i<=count($color); $i++){
echo $color [$i];
}

3.4 Constructors

The constructors are functions that supply values to variables from the class, for instance if we have proprierties in the class we can fuel them with three values in constructors. Of course we can create as many constructors as we need:
class Cookie {
public $quantity;
public function__construct($qty) {
$this→quantity = $quantity;
}
// $this→quantity is our class variable and $qty is variable that will receive real value from outside the class
}

Comments

Popular posts from this blog

Intro

Intro .php PHP code will be put in a file with extension .php . The most important php file is index.php which is also the page that the server searches when the site is accessed by users. When the user searches inside his browser for a specific website, the browser sends a request to the server where the site is stored, and more precisely to the index.php page of the site, which is the gateway to the site. Usually index.php file is placed in the root folder of the website. It is mandatory to insert opening php tag before coding. If html code is not followed by the php code then closing tag may be missing. Best practices tell us that at the end of the script we should put php closing tag: <?php  // code   ?> Comments The comments are very important for programmer. If we want to comment one single line we should use double slash (//) in front of the code or #sign.   <?php   // $number = 3;   # $number = 3;   ?> If there ...