1021 posts
  • Has been part of the Envato Community for over 1 year
  • Has referred 1+ members
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
+4 more
uziiuzair says

So I’m finally learning PHP :D
Through Codecademy that is.

The problem I am having is in Objects :/
I dont know if my code is correct (its giving an error but the system is defining it as correct) :/
Plus I do not understand how the code is working.

<?php

            class Person {

                public $isAlive = true;

                public $firstname;
                public $lastname;
                public $age;

                public function __constructor($firstname, $lastname, $age) {

                    $firstname->firstname = $firstname;
                    $lastname->lastname = $lastname;
                    $age->age = $age;

                }

                public function greet() {
                    return "Hello, my name is " . $firstname->firstname . " " . $lastname->lastname . ". Nice to meet you! :-)";
                }

            }

            $teacher = new Person("boring", "12345", 12345);
            $student = new Person("boring", "12345", 12345);

            echo $teacher->isAlive;
            echo $student->age;

            echo $teacher->greet();
            echo $student->greet()

        ?>


Now since this site can not explain to me what the step by step process is, can someone guide me through?

this is the error im getting:
1Undefined variable: firstname (line 31)Trying to get property of non-object (line 31)Undefined variable: lastname (line 31)Trying to get property of non-object (line 31)Hello, my name is . Nice to meet you! :-)

So that completely shows how ugly my error is. Any help?

240 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 1+ items on Envato Market
  • Has been part of the Envato Community for over 4 years
+2 more
phpdude says

You need save the $firstname variables into the class’s public variables by doing this instead:

$this->firstname = $firstname; $this->lastname = $lastname; $this->age = $age;


Then when you need to use that variable, you use $this->firstname instead of $firstname->$firstname

1509 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
OriginalEXE says
Change:
public function greet() {
    return "Hello, my name is " . $firstname->firstname . " " . $lastname->lastname . ". Nice to meet you! :-)";
}

to this:

public function greet() {
    return "Hello, my name is " . $this->firstname . " " . $this->lastname . ". Nice to meet you! :-)";
}
and this:
public function __constructor($firstname, $lastname, $age) {

    $firstname->firstname = $firstname;
    $lastname->lastname = $lastname;
    $age->age = $age;

}

to this:

public function __constructor($firstname, $lastname, $age) {

    $this->firstname = $firstname;
    $this->lastname = $lastname;
    $this->age = $age;

}

$this represents our object and you can use it inside that object method to access other methods and properties.

1021 posts
  • Has been part of the Envato Community for over 1 year
  • Has referred 1+ members
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
+4 more
uziiuzair says

Okay so I made the changes but there is no output :/

1Hello, my name is . Nice to meet you! :-)Hello, my name is . Nice to meet you! :-)

And also why is there no output for
echo $student->age;
1509 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
OriginalEXE says
Noticed one more mistake, you have this:
public function __constructor
change to this:
public function __construct
1021 posts
  • Has been part of the Envato Community for over 1 year
  • Has referred 1+ members
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
+4 more
uziiuzair says

Noticed one more mistake, you have this:
public function __constructor
change to this:
public function __construct

Oh thank god! Thanks :D

1021 posts
  • Has been part of the Envato Community for over 1 year
  • Has referred 1+ members
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
+4 more
uziiuzair says
Whats public ? and is there also a private? :s
public $id = "";
Does this exist?
private $id ="";
1509 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
OriginalEXE says

Yes, it does.

You have public, private and protected.

  • public – can be accessed from anywhere.
  • private – can be accessed only from within a class
  • protected – can be accessed from within a class and classes extended from it
So if you were to declare $age like this:
private $age;
this would not work:
echo $student->age;

because you call it outside of the class.

1021 posts
  • Has been part of the Envato Community for over 1 year
  • Has referred 1+ members
  • Has sold $100+ on Envato Market
  • Has collected 10+ items on Envato Market
+4 more
uziiuzair says
So for example if I have 2 php files. index, func
index has a code like:
echo $code;
include_once("func.php");
The $code variable is stored in func.php, would index still output it? Or do I have to include it before echo $code?
1509 posts
  • Has referred 1+ members
  • Has sold $10,000+ on Envato Market
  • Has collected 10+ items on Envato Market
  • Made it to the Authors' Hall of Fame
+2 more
OriginalEXE says

You have to include it before echoing the $code.

Before including it, your domain is undefined and echoing will throw error.

Helpful Information

  • Please read our community guidelines. Self promotion and discussion of piracy is not allowed.
  • Open a support ticket if you would like specific help with your account, deposits or purchases.
  • Item Support by authors is optional and may vary. Please see the Support tab on each item page.

Most of all, enjoy your time here. Thank you for being a valued Envato community member.

Post Reply

Format your entry with some basic HTML. Read the Full Details, or here is a refresher:

<strong></strong> to make things bold
<em></em> to emphasize
<ul><li> or <ol><li> to make lists
<h3> or <h4> to make headings
<pre></pre> for code blocks
<code></code> for a few words of code
<a></a> for links
<img> to paste in an image (it'll need to be hosted somewhere else though)
<blockquote></blockquote> to quote somebody

:grin: :shocked: :cry: Complete List of Smiley Codes

by
by
by
by
by
by