[{"data":1,"prerenderedAt":536},["ShallowReactive",2],{"blog-en-\u002Fblog\u002Forientacao-a-objetos-php":3},{"id":4,"title":5,"body":6,"date":526,"description":527,"extension":528,"meta":529,"navigation":219,"path":530,"seo":531,"stem":532,"tags":533,"__hash__":535},"blog_en\u002Fblog\u002Forientacao-a-objetos-php.md","Object-oriented programming fundamentals in PHP",{"type":7,"value":8,"toc":513},"minimark",[9,18,23,45,55,59,83,86,89,102,106,121,132,136,143,146,156,163,182,188,373,378,427,430,433,436,450,454,457,464,467,471,474,478,490,494,509],[10,11,12,13,17],"p",{},"Notes from the Object Orientation with PHP course at Alura, in Portuguese (which is why the examples use the ",[14,15,16],"code",{},"Alura\\Banco"," namespace).",[19,20,22],"h2",{"id":21},"mvc","MVC",[10,24,25,32,33,38,39,44],{},[26,27,28],"em",{},[29,30,31],"strong",{},"Model"," refers to the layer that models our business — that is, the domain and the business rules. ",[26,34,35],{},[29,36,37],{},"View"," is how that gets represented visually, and the connection between the two is handled by the ",[26,40,41],{},[29,42,43],{},"Controller"," layer.",[46,47,48],"blockquote",{},[10,49,50,51,54],{},"MVC was conceived in the 1980s, during the development of ",[29,52,53],{},"Smalltalk"," applications.",[19,56,58],{"id":57},"the-four-pillars-of-object-orientation","The four pillars of object orientation",[60,61,62,68,73,78],"ol",{},[63,64,65],"li",{},[29,66,67],{},"Abstraction",[63,69,70],{},[29,71,72],{},"Encapsulation",[63,74,75],{},[29,76,77],{},"Inheritance",[63,79,80],{},[29,81,82],{},"Polymorphism",[10,84,85],{},"One of the ways to reuse code is to make a class extend another — that is, inherit everything a base class has and add specific behaviors.",[10,87,88],{},"But it's worth stressing that inheritance is not the solution to every code-reuse problem — it can often even be harmful.",[10,90,91,92,101],{},"It's still not 100% clear to me, but the article ",[93,94,98],"a",{"href":95,"rel":96},"https:\u002F\u002Fwww.alura.com.br\u002Fartigos\u002Fcomo-nao-aprender-orientacao-a-objetos-heranca",[97],"nofollow",[29,99,100],{},"Como não aprender orientação a objetos: Herança"," (in Portuguese) is worth reading. I saved the link to reread later, with more object-orientation experience, and again a few months after that — it tends to make more sense with every reread.",[19,103,105],{"id":104},"access-modifiers","Access modifiers",[10,107,108,109,112,113,116,117,120],{},"In PHP we have three access modifiers for the members (attributes and\u002For methods) defined in our classes: ",[14,110,111],{},"public",", ",[14,114,115],{},"protected"," and ",[14,118,119],{},"private",".",[10,122,123,125,126,128,129,131],{},[14,124,111],{}," lets everyone access the member, including outside the class where it was defined, through an instance. ",[14,127,115],{}," grants access to the current class and its child classes. ",[14,130,119],{}," grants access to the current class only.",[19,133,135],{"id":134},"namespaces","Namespaces",[10,137,138,139,142],{},"By defining namespaces, we can easily have two classes with the same name in different namespaces. A class named ",[14,140,141],{},"Conta"," (account), for example, can have a different meaning in the login module and in the accounts module. Using a root namespace for our application, we also avoid conflicts with external packages we may come to use.",[10,144,145],{},"It is very useful and good practice to use namespaces to separate the classes in our application. To make good use of this feature, there are a few details we should understand.",[10,147,148,149,151,152,155],{},"When we define a class without declaring its namespace, it is created in PHP's default namespace. This namespace has an \"empty name\", meaning a ",[14,150,141],{}," class without a namespace has ",[14,153,154],{},"\\Conta"," as its full name.",[10,157,158,159,162],{},"If, inside a class with a defined namespace, we need to access classes from another namespace, we have to add a ",[14,160,161],{},"use"," statement or spell out the full name (namespace + class name).",[10,164,165,166,169,170,173,174,177,178,181],{},"Being in the ",[14,167,168],{},"Alura\\Banco\\Modelo\\Conta\\Conta"," class, for example, and wanting to access the ",[14,171,172],{},"Alura\\Banco\\CPF"," class, we need to state the full class name, or add ",[14,175,176],{},"use Alura\\Banco\\CPF"," at the top of the file — which lets us use just ",[14,179,180],{},"CPF"," as the name in the rest of the file.",[10,183,184,185,187],{},"Another interesting detail: if we need to \"import\" (with ",[14,186,161],{},") more than one class from the same namespace, we can do it on a single line, wrapping the class names in braces. For example:",[189,190,195],"pre",{"className":191,"code":192,"language":193,"meta":194,"style":194},"language-php shiki shiki-themes darcula","namespace Alura\\Banco\\Conta;\n\nuse Alura\\Banco\\{CPF, Endereco};\nuse \\SplFileInfo;\n\nclass Conta\n{\n    public function umMetodoQualquer()\n    {\n        \u002F\u002F ...\n        $cpf = new CPF();\n        \u002F\u002F ...\n        $endereco = new Endereco();\n        \u002F\u002F ...\n        $fileInfo = new SplFileInfo();\n        \u002F\u002F ...\n        $fileObject = new \\SplFileObject();\n        \u002F\u002F ...\n    }\n}\n","php","",[14,196,197,214,221,229,237,242,251,257,273,279,286,302,307,320,325,338,343,356,361,367],{"__ignoreMap":194},[198,199,202,206,210],"span",{"class":200,"line":201},"line",1,[198,203,205],{"class":204},"sYMgc","namespace",[198,207,209],{"class":208},"sZM-y"," Alura\\Banco\\Conta",[198,211,213],{"class":212},"sE2CK",";\n",[198,215,217],{"class":200,"line":216},2,[198,218,220],{"emptyLinePlaceholder":219},true,"\n",[198,222,224,226],{"class":200,"line":223},3,[198,225,161],{"class":204},[198,227,228],{"class":212}," Alura\\Banco\\{CPF, Endereco};\n",[198,230,232,234],{"class":200,"line":231},4,[198,233,161],{"class":204},[198,235,236],{"class":212}," \\SplFileInfo;\n",[198,238,240],{"class":200,"line":239},5,[198,241,220],{"emptyLinePlaceholder":219},[198,243,245,248],{"class":200,"line":244},6,[198,246,247],{"class":204},"class",[198,249,250],{"class":212}," Conta\n",[198,252,254],{"class":200,"line":253},7,[198,255,256],{"class":212},"{\n",[198,258,260,263,266,270],{"class":200,"line":259},8,[198,261,262],{"class":204},"    public",[198,264,265],{"class":204}," function",[198,267,269],{"class":268},"s2lgc"," umMetodoQualquer",[198,271,272],{"class":212},"()\n",[198,274,276],{"class":200,"line":275},9,[198,277,278],{"class":212},"    {\n",[198,280,282],{"class":200,"line":281},10,[198,283,285],{"class":284},"srRes","        \u002F\u002F ...\n",[198,287,289,293,296,299],{"class":200,"line":288},11,[198,290,292],{"class":291},"sk7qV","        $cpf",[198,294,295],{"class":212}," = ",[198,297,298],{"class":204},"new",[198,300,301],{"class":212}," CPF();\n",[198,303,305],{"class":200,"line":304},12,[198,306,285],{"class":284},[198,308,310,313,315,317],{"class":200,"line":309},13,[198,311,312],{"class":291},"        $endereco",[198,314,295],{"class":212},[198,316,298],{"class":204},[198,318,319],{"class":212}," Endereco();\n",[198,321,323],{"class":200,"line":322},14,[198,324,285],{"class":284},[198,326,328,331,333,335],{"class":200,"line":327},15,[198,329,330],{"class":291},"        $fileInfo",[198,332,295],{"class":212},[198,334,298],{"class":204},[198,336,337],{"class":212}," SplFileInfo();\n",[198,339,341],{"class":200,"line":340},16,[198,342,285],{"class":284},[198,344,346,349,351,353],{"class":200,"line":345},17,[198,347,348],{"class":291},"        $fileObject",[198,350,295],{"class":212},[198,352,298],{"class":204},[198,354,355],{"class":212}," \\SplFileObject();\n",[198,357,359],{"class":200,"line":358},18,[198,360,285],{"class":284},[198,362,364],{"class":200,"line":363},19,[198,365,366],{"class":212},"    }\n",[198,368,370],{"class":200,"line":369},20,[198,371,372],{"class":212},"}\n",[374,375,377],"h3",{"id":376},"topics-to-remember","Topics to remember",[379,380,381,384,387,394,400,411,414,417],"ul",{},[63,382,383],{},"We can split our classes into namespaces, just like we split files into folders.",[63,385,386],{},"By separating classes into namespaces, we can have classes with the same name in different namespaces.",[63,388,389,390,393],{},"Using a root namespace (called a ",[26,391,392],{},"vendor namespace"," in some of the literature), we can avoid conflicts with classes from external packages we may use.",[63,395,396,397,399],{},"To use a class, we need to import it with ",[14,398,161],{}," or state its full name (with namespace) everywhere it is used.",[63,401,402,403,406,407,410],{},"With a PHP autoloader (",[14,404,405],{},"spl_autoload_register","), we can avoid using ",[14,408,409],{},"require"," over and over to include every file the program needs to run.",[63,412,413],{},"An abstract class is not concrete — it is not complete yet and needs to be extended to make sense. That's why we can't instantiate objects directly from abstract classes.",[63,415,416],{},"Abstract methods are a way to \"force\" child classes to implement a given method; only abstract classes can have abstract methods.",[63,418,419,420,423,424,426],{},"The ",[14,421,422],{},"ControladorDeBonificacoes"," class, for example, represents the employee bonus control feature. Through an instance of it, we can add employees and, at the end, retrieve the total spent on bonuses. A model class like ",[14,425,141],{},", on the other hand, represents something that exists in our domain.",[19,428,82],{"id":429},"polymorphism",[10,431,432],{},"This 4th principle is directly tied to the 3rd (inheritance), since it's through inheritance that we achieve polymorphism.",[10,434,435],{},"Polymorphism is the ability of an object to be referenced in multiple ways (careful: it doesn't mean the object keeps transforming itself — quite the opposite, an object is born of one type and dies of that type; what can change is the way we refer to it).",[10,437,438,439,442,443,446,447,449],{},"With that, we can, for example, receive a ",[14,440,441],{},"Funcionario"," (employee) as a parameter in a function or method and pass in a ",[14,444,445],{},"Gerente"," (manager). We can refer to the ",[14,448,445],{}," instance in more than one way, and that can be useful in many cases, as we saw throughout the course.",[19,451,453],{"id":452},"folder-organization","Folder organization",[10,455,456],{},"During the Object Orientation trainings, we organized our folders using a fairly common structure.",[10,458,459,460,463],{},"There is a root folder for our code (",[14,461,462],{},"src","), which also makes it easier to follow PSR-4.",[10,465,466],{},"Inside this root folder, there are subfolders that identify responsibilities in our system.",[19,468,470],{"id":469},"architecture","Architecture",[10,472,473],{},"Organizing the folders in our code is a very simple beginning of a study on architecture. How to organize our project is a very important subject, and there are plenty of books, talks and content in general about it.",[19,475,477],{"id":476},"alternatives-to-our-approach","Alternatives to our approach",[10,479,480,481,112,483,486,487,489],{},"It is very common — and recommended by many — that instead of separating the subfolders by responsibility (",[14,482,31],{},[14,484,485],{},"Service",", etc.), we separate them by domain concepts, similar to what we did inside the ",[14,488,31],{}," folder. Dedicated studies on architecture and code design cover these concepts in much more detail.",[19,491,493],{"id":492},"relationships-between-models","Relationships between models",[10,495,496,497,112,500,112,503,112,506,120],{},"Through method definitions, we declare the relationships, returning the type of that relationship. For example: ",[14,498,499],{},"belongsTo",[14,501,502],{},"hasMany",[14,504,505],{},"hasOne",[14,507,508],{},"belongsToMany",[510,511,512],"style",{},"html pre.shiki code .sYMgc, html code.shiki .sYMgc{--shiki-default:#CC7832}html pre.shiki code .sZM-y, html code.shiki .sZM-y{--shiki-default:#6897BB}html pre.shiki code .sE2CK, html code.shiki .sE2CK{--shiki-default:#A9B7C6}html pre.shiki code .s2lgc, html code.shiki .s2lgc{--shiki-default:#FFC66D}html pre.shiki code .srRes, html code.shiki .srRes{--shiki-default:#808080}html pre.shiki code .sk7qV, html code.shiki .sk7qV{--shiki-default:#9876AA}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}",{"title":194,"searchDepth":216,"depth":216,"links":514},[515,516,517,518,521,522,523,524,525],{"id":21,"depth":216,"text":22},{"id":57,"depth":216,"text":58},{"id":104,"depth":216,"text":105},{"id":134,"depth":216,"text":135,"children":519},[520],{"id":376,"depth":223,"text":377},{"id":429,"depth":216,"text":82},{"id":452,"depth":216,"text":453},{"id":469,"depth":216,"text":470},{"id":476,"depth":216,"text":477},{"id":492,"depth":216,"text":493},"2022-12-19","Notes from Alura's Object Orientation with PHP course: MVC, the four pillars, access modifiers, namespaces and folder organization.","md",{},"\u002Fblog\u002Forientacao-a-objetos-php",{"title":5,"description":527},"blog\u002Forientacao-a-objetos-php",[193,534,469],"oop","DZT_D8kHGpWgakT11ZsUBKMMHxWFH1PV6c9SpeICpp0",1794928450126]