Thank you both really again and again for your help. I will read again and again this evening your comments, as Iâm understand better now the things ( I hope ).
A very quick question, before get back to you both, why when put the following code at the end of the code provided by Hall_of_Famer /
$hello = new Names(" Apple "," Born "," Content "," Document ");
echo $hello->toArray(2);
I get just the word (Array) in the browser. Normaly the work (Content) that should appear, No ?
Well of course you got the word array to browser, since in PHP array cannot be converted to string automatically. To fix this, just change the last line to:
About Codeacademy, I like it and test it also, but it is still âlightâ as content, teaching just the basic. In fact, I learn now the tutorial of Kevin Skoglund on Lynda.com ( PHP with MySQL Essential Training ). He is really explaining slowly each thing in PHP during the 131 videos of his course. So I got an general idea about PHP and its variable, functions, arrays, and still learning. But you know, I like to make some small codes to understand the things more, and I come to here to share my experience.
Iâm really have the chance to get the help of SitePointâ members here, like Hall_of_Famer, megazoid, and otherâŚtheir way to support learners like me is really very helpful.
Great you are learning, but I just quickly took in the course titles and it doesnât look like Kevin is teaching OOP practices. In fact, in this preview video, Kevin is saying take his subject CRUD scripts and do a copy and paste of the code for the page CRUD scripts. That is exactly what you should be avoiding, when working with OOP. âCopy and Paste codingâ. This form of coding is no longer the standard anymore (was it ever?). So, although you may learn a few things about PHP in those videos, you are not learning how to do OOP.
So, although the Codeacademy exercises are basic, that is where you really need to start. Or with any other tutorial that teaches the core concepts of OOP. Once you get the basics down, then try your hand at coding something in OOP.
Exactly, and all what you talk about it is true. He is just talking about PHP, but I will learn other courses about OOP, as I like Laravel and choose it as framwork to built with my first future app with OOP.
You mean that Codeacademy will teach me also OOP during its lessons ?
It is OK, I found the answer on Codeacademy.
Iâm on this website from 2 hours and half, and now reading about
âWhatâs Object-Oriented Programming?â
So I understand now s_molinari why you advice me to go to this website. Thanks !
Back to what you ( Hall of Famer) said before about properties /
I was learning yesterday on codeacademy following the advice of e_molinari /
They made in their example to present OOP, the following coed /
<!DOCTYPE html>
<html>
<head>
<title> Introduction to Object-Oriented Programming </title>
<link type='text/css' rel='stylesheet' href='style.css'/>
</head>
<body>
<p>
<?php
// The code below creates the class
class Person {
// Creating some properties (variables tied to an object)
public $isAlive = true;
public $firstname;
public $lastname;
public $age;
// Assigning the values
public function __construct($firstname, $lastname, $age) {
$this->firstname = $firstname;
$this->lastname = $lastname;
$this->age = $age;
}
// Creating a method (function tied to an object)
public function greet() {
return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
}
}
// Creating a new person called "boring 12345", who is 12345 years old ;-)
$me = new Person('boring', '12345', 12345);
// Printing out, what the greet method returns
echo $me->greet();
?>
</p>
</body>
</html>
As you can see, they were made the properties of Person class as Public, are you are with that ? It is very important for me to know why you choose Private and they choose Public ?
I looked up the code in academy, it is like your very first OOP example. The reason why I use private is that, it is better encapsulation. But encapsulation is an intermediate-level concept, and not every beginner understands it. If you read Matt Zandstraâs book PHP Objects, Patterns and Practices, he also illustrated the very basics of PHP with public properties. But even since access modifiers and encapsulation were introduced, he would use private or protected properties from then on.
Since you are an absolute beginner, you may not worry about access modifiers and encapsulation, just do what code academy is telling you. But once you get to the next stage, you should always keep your properties private. OOP is more than just using object oriented syntax, keep this in mind.
As @Hall_of_Famer mentioned, since you are beginning, donât worry too much yet about why it might be better to have private properties above public ones. All you need to understand is what the difference is between public, protected and private properties and methods and the differences there are between them in relation to object and application scope. This is called âvisibilityâ. Once you get that down, along with the other basics of OOP, then you can move on to the next level of concepts, like polymorphism, inheritance, encapsulation, SOLID, etc. Once you get that down, then you can move to design patterns. I can also recommend the book Hall_of_Famer mentioned from Matt Zandstra, once you get to that level, although, it also helps with the intermediate level too. Just get it. It will be good reading for you.
Hello all,
I really miss your advices.
Ok Iâm learning now the book, and finished the php course on codecademy.
I want to show you what I did ( just to learn) and I enjoyed 2 things /
A : The fact that I did the small idea that I want to do
B : The fact that I touched how much your are nice people before being developers, so Thanks again for your advices.
<?php
require ('_showerrors.php');
class Names{
private $a;
private $b;
private $c;
private $d;
public function __construct($a,$b,$c,$d){
$this->a = $a;
$this->b = $b;
$this->c = $c;
$this->d = $d;
}
public function toArray(){
$myArray = array($this->a, $this->b, $this->c, $this->d);
shuffle ($myArray);
return $myArray;
}
}
$hello = new Names(" Apple "," Born "," Content "," Document ");
$values = $hello->toArray();
echo "A random result : <br>";
foreach($values as $val) {
echo $val.', ';
}
?>
Great #megazoid, your advices are always welcome, thanks you !!
VoilĂ , I just read a new thing. By the way, a question :
Should we (the learners who seek to become developers) memorize all the functions in PHP, to be able to make for example an interactive website or application ? Or we could just memorize the most used functions, and search for others over the web when we need ?
Seriously though, I think it is a good idea to look around the documentation to get an overview of what is there.
That way when you need to do something youâll have an idea of where to look.
In my experience, âknowingâ the functions isnât a matter of memorizing them, itâs more a matter of using them often enough that you eventually just know them.
Just knowing what types of functions there are should be sufficient. If you know there is a function that might do what you want then you can always look it up.
When writing some part of code you just should think âhmm⌠this operation looks very common, they probably already have a function/library for thatâ. For example, you want to reverse order of elements in your array. How do you think, how many other programmers might want to do that in their code too? A lot, probably. So you can simply google âphp reverse arrayâ and itâll bring a link to array_reverse() function for you immediately. I think this is a good practice to google before writing code. It saves a lot of time often, especially when youâre beginner.
Hahah who knows, maybe I will be able to develop a quiz soon
I like your advice, thank you !
I like your advice too, thank you !
I like always the way you explain the things. When you say âfor exampleâ, the text after those 2 words, will often be very helpful, and this is the way actually that Iâm using to learn more, and to practice. I thought before that it is not a good way to think, or maybe just a personal solution that I found, BUT you just confirm me that it is good to google it and find available solutions before reinventing the wheel. thank you !