Podstawy PHP
Przydatne linki
- PHP The Right Way
Strona, na której zamieszczone są wskazówki dotyczące pisania prawidłowego kodu PHP.
Framework Laravel
MVC - Model View Controller
- The model - The domain that your software is built around. Models are based on real-world items such as a person, bank account, or product. If you were building a blog, your models might be post and comment. Models are typically permanent and will be stored outside the application, often in a database. A model is more than just data; it enforces all the business rules that apply to that data. For example, if a discount shouldn’t be applied to orders of less than $10, the model will enforce the constraint. This makes sense; by putting the implementation of these business rules in the model, we make sure that nothing else in the application can make our data invalid. The model acts as both a gatekeeper and a data store.
- The view - The visual representation of a model, given some context. It’s usually the resulting markup that the framework renders to the browser, such as the HTML representing the blog post. The view layer is responsible for generating a user interface, normally based on data in the model. For example, an online store will have a list of products to be displayed on a catalog screen. This list will be accessible via the model, but it will be a view that accesses the list from the model and formats it for the end user. Although the view may present the user with various ways of inputting data, the view itself never handles incoming data. The view’s work is done once the data is displayed.
- The controller - The coordinator that provides the link between the view and the model. The controller is responsible for processing input, acting upon the model, and deciding on what action should be performed, such as rendering a view or redirecting to another page. Continuing the blog example, the controller might look up the most recent comments for a post (the model) and pass them to the view for rendering.
Podstawowe funkcje
Instrukcja switch
switch($i): case 0: break; default: echo "Hey"; endswitch;
Pętla foreach
foreach ($arr as $i) { //code }
Pętla while
while(//cond): //code endwhile;
Funkcja unset
unset($i);
Operacje na zmiennych typu string
strlen($i); substr($i, pos1, pos2); strtoupper($i); strtolower($i); strpos($i, "x");
Biblioteka math
round(M_PI);
Tablice array
array_push($arr, var); count($arr); sort($arr); rsort($arr); join(",", $arr);
Tablice asocjacyjne
$myAssocArray = array('year' => 2012, 'colour' => 'blue', 'doors' => 5, 'make' => 'BMW'); echo $myAssocArray['make'];
Tablice wielowymiarowe
$deck = array(array('2 of Diamonds', 2), array('5 of Diamonds', 5), array('7 of Diamonds', 7), array('LOL',1)); echo 'You have the ' . $deck[2][0] . '!';
Średniozaawansowany poziom
Klasy i ich konstruktory
//CLASS class Person { public $age; //CONSTRUCT public function __construct($firstname, $lastname, $age) { $this->firstname=$firstname; $this->lastname=$lastname; $this->age=$age; } } $teacher = new Person();
Wyświetlanie właściwości klas
echo $teacher->isAlive;
Funkcje klasowe is_a, property_exist, method_exists
//IS_A, PROPERTY_EXISTS, METHOD_EXISTS $me = new Person("Shane"); if (is_a($me, "Person")) { echo "I'm a person, "; } if (property_exists($me, "name")) { echo "I have a name, "; } if (method_exists($me, "dance")) { echo "and I know how to dance!"; }
Dziedziczenie / inheritance
class Square extends Shape { final public function honk() { return "HONK HONK!"; } }
Metody statyczne
class Person { static public function say() { echo "Here are my thoughts!"; } }
Właściwości statyczne
//USING STATIC OF CLASSES class Blogger extends Person { const cats = 50; }
Wyświetlanie przez różne wywoływanie metod
Blogger::say(); echo Blogger::cats;