Hello,

I try to create a simple OOP PHP that shows an error:

class_lib.php

<?php

class person {

var $name;

function __constructor($person_name){
    echo "Initialize class";
}

function set_name($new_name){
    $this->name($new_name);
}

function get_name($new_name){
    return $this->name($new_name);  
}

function __destructor(){
    echo "end class";
}
}

?>

program3.php

<?php include("class_lib.php"); ?> <?php

$stefan = new person();
$jimmy = new person();

$stefan->set_name();
$jimmy->set_name();

echo "Stefan Full Name : ".$stefan->get_name();
echo "Jimmy Full Name : ".$jimmy->get_name();

?>

Fatal error: Uncaught ArgumentCountError: Too few arguments to function person::set_name(), 0 passed in C:\xampp\htdocs\TEST\program_3_OOP_PHP.php on line 10 and exactly 1 expected in C:\xampp\htdocs\TEST\class_lib.php:14 Stack trace: #0 C:\xampp\htdocs\TEST\program_3_OOP_PHP.php(10): person->set_name() #1 {main} thrown in C:\xampp\htdocs\TEST\class_lib.php on line 14

line 14: function set_name($new_name){

I am following this tutorial: https://belajarphp.net/belajar-konsep-oop-php/

Thanks in advance.
Can anyone help me fix the error?

Recommended Answers

All 7 Replies

$stefan->set_name('stefan');
$jimmy->set_name('jimmy');

Edit __constructor to __construct and __destructor to __destruct.
If you define function with required argument then you cant initialize it without argument e.g.
Constructor with required argument:

function __construct($person_name){
    echo "Initialize class";
    $this->set_name($person_name);
}

Same contructor but argument is optional (set default value):

function __construct($person_name=NULL){
    echo "Initialize class";
    if($person_name !== NULL){
        $this->set_name($person_name);
    }
}

I still getting this error:

Fatal error: Uncaught ArgumentCountError: Too few arguments to function person::__construct(), 0 passed in C:\xampp\htdocs\TEST\program_3_OOP_PHP.php on line 7 and exactly 1 expected in C:\xampp\htdocs\TEST\class_lib.php:10 Stack trace: #0 C:\xampp\htdocs\TEST\program_3_OOP_PHP.php(7): person->__construct() #1 {main} thrown in C:\xampp\htdocs\TEST\class_lib.php on line 10

class_lib.php

<?php

class person {

var $name;

function __construct($person_name){
    echo "Initialize class";
}

function set_name($new_name){
    $this->name($new_name);
}

function get_name($new_name){
    return $this->name($new_name);  
}

function __destruct(){
    echo "end class";
}
}

?>

program3.php

<?php include("class_lib.php"); ?>

<?php

$stefan = new person();
$jimmy = new person();

$stefan->set_name('stefan');
$jimmy->set_name('jimmy');

echo "Stefan Full Name : ".$stefan->get_name();
echo "Jimmy Full Name : ".$jimmy->get_name();

?>

... or call constructor with param:

$stefan = new person('Stefan');

Also edit functions:

function set_name($new_name){
    $this->name = $new_name;
}

and

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

you can call destructor by set object to NULL e.g.

$stefan = new person();
$jimmy = new person();
$stefan->set_name('stefan');
$jimmy->set_name('jimmy');
echo "Stefan Full Name : ".$stefan->get_name();
echo "Jimmy Full Name : ".$jimmy->get_name();

$stefan = NULL;
$jimmy = NULL;

in your constructor code you ask for a parameter in this case function __construct($person_name){
you're not actually setting person_name in that method but you are requiring it

you should either remove it function __construct(){ from the constructor or default it to null function __construct($person_name=null){

if you keep it you should call set_name($person_name) within your constructor

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.