Who could illustrate PHP?

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:

echo print_r($hello->toArray(), TRUE);

Thanks to your help, the script works very good now ( in fact, without you, I have not had the opportunity to get it work)

<?php
error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
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(){
		return array($this->a, $this->b, $this->c, $this->d);
	}
}

$hello = new Names(" Apple "," Born "," Content "," Document ");

foreach ($hello->toArray() as $result){
	echo $result ;
  }

?>

I’m trying now to display the 4 words randomly, but to be honest, it is not a simple issue, I will back to you soon.

Many thanks again…

Codeacademy has a fairly good interactive tutorial to get your feet wet with PHP OOP.

There is also a good bit of material to take in here too.

Scott

1 Like

Thank you for your advice

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.

Scott

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 ?

Thank again for your interest.

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 !

Hello all,

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 ?

Thanks !

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.

3 Likes

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. :smile:

2 Likes

Honestly, thank you both for your advices,I will do that, means, continue the learning level by level + finding this book

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.', ';
}
?>

Hi

A little tip. PHP has a special function to do what this part of your code does:

foreach($values as $val) {
    echo $val.', ';
}

That function is implode:

echo implode(', ', $values);
1 Like

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 ?

LOL, AFAIK there will not be a quiz later. :wink:

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.

1 Like

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.

1 Like

That’s absolutely true.

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.

3 Likes

Hahah who knows, maybe I will be able to develop a quiz soon :wink:

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 !

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.