PHP Syntax Issue

<?php 
function my_autoload($class){
	include 'classes/'.$class.'.class.php';
}
spl_autoload_register('my_autoload');

$bike = new Bicycle;
$bike->brand = 'Trek';
echo $bike->brand;

I am getting bug here →

However, if I try to put the code here:

That doesn’t show any bug.

Nobody knows what that means. What error do you get?

Here

I have generally seen that pages do not load with that error when there is some syntax error →

when I used this → ini_set(‘display_errors’, 1);

I was able to get to the error.

biycle.class.php was the culprit, but actually, it should have been: Bicycle.class.php

Capital Letter → B

Why not use PHP error reporting functions instead of a third-party application?

I tried to find the bug by adding a empty Class and it worked fine?

<?php declare(strict_types=1);
error_reporting(E_ALL);
ini_set('display_errors', 'true');

#===========================
function my_autoload($class)
{
  include 'classes/'.$class.'.class.php';
}
spl_autoload_register('my_autoload');

$bike = new Bicycle;
$bike->brand = 'Trek';
echo $bike->brand;

I also incorrectly renamed the class and the following errors were thrown:

Warning: include(classes/Bicycle.class.php): failed to open stream: No such file or directory in /var/www/AATEST/codeispoetry/bug/index-001.php on line 8

1 Like

OffTopic:
Is it a better coding notion to have class name’s letters capitalized:?
ClassName → C and N, for example.

Take a look at the PHP Manual

I prefer prefixing my class names with Class_whatever.php so that they are easier to find in my editor’s list of open files

1 Like

PHP’s informal coding standard says that indeed it should be ClassName - see https://www.php-fig.org/psr/psr-1/#3-namespace-and-class-names

In addition to PSR-1 there is also PSR-12: https://www.php-fig.org/psr/psr-12/

2 Likes

Whatever naming style you choose, the key is to be consistant with it. That is how you avoid these little errors.

True. Thanks for highlighting.

Dont we have any smart code for this function:

function my_autoload($class){
	include 'classes/'.$class.'.class.php';
}
spl_autoload_register('my_autoload');

The class name may be like this, starting with →

class Bicycle {
	public $brand;
	public $model;
	public $year;
	public $description = 'Used bicycle';	
	private $weight_kg = 0.0;
	protected $wheels = 2;
}

but the file name should be with small letter bicycle.class.php, but it should pull the class definition with either capital or small letter whatever is written in the class definition.

Means the function should search for both:
Bicycle.class.php
and
bicycle.class.php

I personally feel mental uneasiness when the file name begins with a capital letter.

Also advise it if it is making code too complicated and may be a stupid thing to consider.

I would recommend not rolling things like this yourself but using composer with psr-4 for it.

That way you only need to include a file from composer and composer will do all the magic for you. Plus if you ever want to add a composer package to your project you’re already all set up to go.

Basically PSR-4 will establish a mapping between classes and namespaces in PHP to files and directories in your filesystem, making it very predictable where one could find certain classes.

I had the same, but I can assure life will be much easier when you get over this and go to a system where a class maps to a file unambiguously, so that if I see classname in a namespace I can go find the file where that’s defined easily.

Preferring explicitness over convention is much better for your sanity in the long run - and not just where naming of classes are concerned.

2 Likes

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