M
M
Maks T2015-04-27 11:24:14
PHP
Maks T, 2015-04-27 11:24:14

What to read to understand the OPP syntax in PHP?

Good afternoon toasters!
I came across an article
habrahabr.ru/post/143317
Since I am self-taught in PHP, I do not understand such moments in syntax:

$jsonError->error='No function called';

How can you create an object without creating a class??
I watch various videos, they often use the this return in the class
return $this
.
the meaning of $this is not explained anywhere.
Tell me what literature to read in order to understand PHP OPP.
I myself read Michelle E. Davis and John A. Phillips - Learning PHP and MySQL, PHP_Praktika_sozdania_Web-saytov_2_izdanie (not all yet).
Every day I look at the manual on php.net + a bunch of different forums.
Everywhere there is a lot of information about encapsulation, about predefined class methods, about built-in functions for working with classes ...
And no one speaks to explain the basics.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
C
Cat Anton, 2015-04-27
@MaximT

Forums and documentation are good, but if you are not familiar with OOP and are just starting out, it's better to read books. To comprehend the basics, I recommend:
PHP 5/6 Tutorial, Kuznetsov, Simdyanov
PHP 5 (original)
If you do not like to read big thick books, then you can just read the chapter on OOP in any of these books.
Regarding c $jsonError->error='No function called';, PHP implicitly creates a stdClass object in this case . Doing so is not recommended as PHP will issue a warning:
Warning: Creating default object from empty value .
It's better to write like this:

<?php
header('Content-type: text/html; charset=UTF-8');
if (count($_REQUEST)>0){
    // ...
}else{
    $jsonError = new stdClass();
    $jsonError->error = 'No function called';
    echo json_encode($jsonError);
}
?>

The result will be the same, but there will be no more warnings.
https://php.net/manual/en/language.oop5.basic.php
The $this pseudo-variable is available if the method was called in an object context. $this is a reference to the called object. This is usually the object that the called method belongs to, but it can be another object if the method was called statically from the context of another object.
return $this- this is done to implement chains of calls (Method Chaining).
www.wisereport.ru/method_chaining

A
asd111, 2015-04-27
@asd111

PHP. Objects, Patterns and Programming
Techniques Russian version is slightly outdated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question