• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

Getting into PHP

FakeJR

Coffee?
Joined
Nov 25, 2014
Messages
128
Reaction score
38
This is my first attempt to create just something in PHP.

<?php

$names = array('Chris' , 'Tom' , 'Bob');
$yrs = array('Chris'=>18 , 'Tom'=>13 , 'Bob'=>15);

echo 'My name is ' . $names[1] . ' and im ' . $yrs[Tom] . ' yrs old.';

?>

But I got this error: Notice: Use of undefined constant Tom - assumed 'Tom' in C:\xampp\htdocs\---\test.php on line 6

Note: I know this is really easy to make but i've never done it before.

Also: Could you link some good tutorials in the comments if you do comment would really appreciate that. :)
 
should be
PHP:
echo 'My name is ' . $names[1] . ' and im ' . $yrs['Tom'] . ' yrs old.';
 
or
Code:
<?php

$names = array('Chris' , 'Tom' , 'Bob');
$yrs = array('Chris'=>18 , 'Tom'=>13 , 'Bob'=>15);


echo 'My name is ' . $names[1] . ' and im ' . $yrs[$names[1]] . ' yrs old.';

?>
 
Code:
<?php
    $people = [ 'Chris' => 18, 'Tom' => 13, 'Bob' => 15 ];
    echo 'My name is '.array_keys($people)[1].' and im '.array_values($people)[1].' yrs old.';
?>
 
PHP:
<?php
$people = [
    [ 'name' => 'Chris',  'age' => 18 ],
    [ 'name' => 'Tom',    'age' => 17 ],
    [ 'name' => 'Bob',    'age' => 15 ]
];
echo sprintf('My name is %s and I\'m %d yrs old.', $people[1]['name'], $people[1]['age']);
 
there are many free tutorials even with php manual in the Internet, don't spend money on books
I know theres free tutorials, and I wasn't gonna buy a "book" to learn this lol.
Thanks anyway.
 
PHP:
<?php
    class People{

        protected $name;
        protected $age;
      
        function __construct($name, $age) {
            $this->name = $name;
            $this->age = $age;
        }
    }
  
    class Person extends People{

        function __construct($name, $age) {
            parent::__construct($name, $age);
        }
      
        public function getPerson(){
            echo sprintf('My name is %s and I\'m %d yrs old.', $this->name, $this->age);
        }
    }
  
    $tom = new Person('Tom', 17);
  
    $tom->getPerson();
?>
 
PHP:
<?php
class Person {
    protected $name;
    protected $age;
 
    public function __construct($name, $age) {
        $this->
            setName($name)
            setAge($age)
        ;
    }
 
    public function getName() {
        return $this->name;
    }
 
    public function setName($name) {
        $this->name = $name;
        return $this;
    }
 
    public function getAge() {
        return $this->age;
    }
 
    public function setAge($age) {
        $this->age = $age;
        return $this;
    }
 
    public function __toString() {
        return sprintf('My name is %s and I\'m %d yrs old.', $this->getName(), $this->getAge());
    }
}

$Chris = new Person('Chris', 24);

echo $Chris;

@Chris
corrected :D
 
Last edited:
PHP:
<?php
class Person {
    protected $name;
    protected $age;

    public function __construct($name, $age) {
        $this->
            setName($name)
            setAge($age)
        ;
    }

    public function getName() {
        return $this->name;
    }

    public function setName($name) {
        $this->name = $name;
        return $this;
    }

    public function getAge() {
        return $this->age;
    }

    public function setAge($age) {
        $this->age = $age;
        return $this;
    }

    public function __toString() {
        return sprintf('My name is %s and I\'m %d yrs old.', $this->getName(), $this->getAge());
    }
}

$Chris = new Person('Chris', 24);

echo $Chris;

@Chris
corrected :D
Just quick question why make age & name protected if there is no intention of inheritance you could have just made them private?

I made them protected because there is 2 classes a parent (People) and child (Person)

Lets simplify your class so that there is only 1 method per property to set and get it.
PHP:
<?php
class Person {
    private $name;
    private $age;
    public function __construct($name, $age) {
        $this->Name($name);
        $this->Age($age);
    }

        public function Name($name=null) {
        $this->name = isset($name) ? $name : $this->name;
        return $this->name;
        }
        public function Age($age=null) {
            $this->age = isset($age) ? $age : $this->age;
            return $this->age;
        }
    public function __toString() {
        return 'My name is '.$this->Name().' and I\'m '.$this->Age().' yrs old.';
    }
}

$Chris = new Person('Chris', 24);

echo $Chris; // My name is Chris and I'm 24 yrs old.
?>

The name & age method do not need to be public to be used in __toString(), they can be private or protected as well.
 
Last edited:
Just quick question why make age & name protected if there is no intention of inheritance you could have just made them private?

I made them protected because there is 2 classes a parent (People) and child (Person)

Lets simplify your class so that there is only 1 method per property to set and get it.
PHP:
<?php
class Person {
    private $name;
    private $age;
    public function __construct($name, $age) {
        $this->Name($name);
        $this->Age($age);
    }

        public function Name($name=null) {
        $this->name = isset($name) ? $name : $this->name;
        return $this->name;
        }
        public function Age($age=null) {
            $this->age = isset($age) ? $age : $this->age;
            return $this->age;
        }
    public function __toString() {
        return 'My name is '.$this->Name().' and I\'m '.$this->Age().' yrs old.';
    }
}

$Chris = new Person('Chris', 24);

echo $Chris; // My name is Chris and I'm 24 yrs old.
?>

The name & age method do not need to be public to be used in __toString(), they can be private or protected as well.
why did I make them protected? well it's simple only library funcionality should be private, it's good habbit
also it's good to take just one naming convention,
what's more everything should be as simple as it's possible so your getters/setters don't follow this rule
anyway I'm not saying your code isn't good
 
Last edited:
why did I make them protected? well it's simple only library funcionality should be private, it's good habbit
also it's good to take just one naming convention,
what's more everything should be as simple as it's possible so your getters/setters don't follow this rule
anyway I'm not saying your code isn't good
This is your class's method output of setName($name) when I use print_r
PHP:
Person Object
(
    [name:protected] => Chris
    [age:protected] => 24
)
This is my modified version of your class and its method Name() when I use print_r
PHP:
Chris

Which is more secure?
 
Last edited:
This is your class's method output of setName($name) when I use print_r
PHP:
Person Object
(
    [name:protected] => Chris
    [age:protected] => 24
)
This is my modified version of your class and its method Name() when I use print_r
PHP:
Chris

Which is more secure?
well... It's called method chaining(read about Fluent Interface), you don't know it?
 
there are many free tutorials even with php manual in the Internet, don't spend money on books
books > anything.
Books are resourceful, plus they tends to induce yourself to develop your own ideas on ways of approaching things instead of going on what people says.
I have learned a lot on internet's tutorials, but I do not change my piles of books for nothing.
 
Back
Top